Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [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 "quiche/quic/core/quic_connection.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 9 | #include <cstdint> |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10 | #include <memory> |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "absl/base/macros.h" |
| 15 | #include "absl/strings/str_cat.h" |
| 16 | #include "absl/strings/str_join.h" |
| 17 | #include "absl/strings/string_view.h" |
| 18 | #include "quiche/quic/core/congestion_control/loss_detection_interface.h" |
| 19 | #include "quiche/quic/core/congestion_control/send_algorithm_interface.h" |
| 20 | #include "quiche/quic/core/crypto/null_decrypter.h" |
| 21 | #include "quiche/quic/core/crypto/null_encrypter.h" |
| 22 | #include "quiche/quic/core/crypto/quic_decrypter.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 23 | #include "quiche/quic/core/frames/quic_connection_close_frame.h" |
| 24 | #include "quiche/quic/core/frames/quic_new_connection_id_frame.h" |
| 25 | #include "quiche/quic/core/frames/quic_path_response_frame.h" |
| 26 | #include "quiche/quic/core/frames/quic_rst_stream_frame.h" |
| 27 | #include "quiche/quic/core/quic_connection_id.h" |
| 28 | #include "quiche/quic/core/quic_constants.h" |
| 29 | #include "quiche/quic/core/quic_error_codes.h" |
| 30 | #include "quiche/quic/core/quic_packet_creator.h" |
| 31 | #include "quiche/quic/core/quic_packets.h" |
| 32 | #include "quiche/quic/core/quic_path_validator.h" |
| 33 | #include "quiche/quic/core/quic_types.h" |
| 34 | #include "quiche/quic/core/quic_utils.h" |
| 35 | #include "quiche/quic/core/quic_versions.h" |
| 36 | #include "quiche/quic/platform/api/quic_expect_bug.h" |
| 37 | #include "quiche/quic/platform/api/quic_flags.h" |
| 38 | #include "quiche/quic/platform/api/quic_ip_address.h" |
bnc | 96dc178 | 2022-09-06 08:16:01 -0700 | [diff] [blame] | 39 | #include "quiche/quic/platform/api/quic_ip_address_family.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 40 | #include "quiche/quic/platform/api/quic_logging.h" |
| 41 | #include "quiche/quic/platform/api/quic_socket_address.h" |
| 42 | #include "quiche/quic/platform/api/quic_test.h" |
| 43 | #include "quiche/quic/test_tools/mock_clock.h" |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 44 | #include "quiche/quic/test_tools/mock_connection_id_generator.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 45 | #include "quiche/quic/test_tools/mock_random.h" |
| 46 | #include "quiche/quic/test_tools/quic_coalesced_packet_peer.h" |
| 47 | #include "quiche/quic/test_tools/quic_config_peer.h" |
| 48 | #include "quiche/quic/test_tools/quic_connection_peer.h" |
| 49 | #include "quiche/quic/test_tools/quic_framer_peer.h" |
| 50 | #include "quiche/quic/test_tools/quic_packet_creator_peer.h" |
| 51 | #include "quiche/quic/test_tools/quic_path_validator_peer.h" |
| 52 | #include "quiche/quic/test_tools/quic_sent_packet_manager_peer.h" |
| 53 | #include "quiche/quic/test_tools/quic_test_utils.h" |
| 54 | #include "quiche/quic/test_tools/simple_data_producer.h" |
| 55 | #include "quiche/quic/test_tools/simple_session_notifier.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 56 | #include "quiche/common/simple_buffer_allocator.h" |
| 57 | |
| 58 | using testing::_; |
| 59 | using testing::AnyNumber; |
| 60 | using testing::AtLeast; |
| 61 | using testing::DoAll; |
| 62 | using testing::ElementsAre; |
| 63 | using testing::Ge; |
| 64 | using testing::IgnoreResult; |
| 65 | using testing::InSequence; |
| 66 | using testing::Invoke; |
| 67 | using testing::InvokeWithoutArgs; |
| 68 | using testing::Lt; |
| 69 | using testing::Ref; |
| 70 | using testing::Return; |
| 71 | using testing::SaveArg; |
| 72 | using testing::SetArgPointee; |
| 73 | using testing::StrictMock; |
| 74 | |
| 75 | namespace quic { |
| 76 | namespace test { |
| 77 | namespace { |
| 78 | |
| 79 | const char data1[] = "foo data"; |
| 80 | const char data2[] = "bar data"; |
| 81 | |
| 82 | const bool kHasStopWaiting = true; |
| 83 | |
| 84 | const int kDefaultRetransmissionTimeMs = 500; |
| 85 | |
| 86 | DiversificationNonce kTestDiversificationNonce = { |
| 87 | 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', |
| 88 | 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', |
| 89 | 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', |
| 90 | }; |
| 91 | |
| 92 | const StatelessResetToken kTestStatelessResetToken{ |
| 93 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 94 | 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}; |
| 95 | |
| 96 | const QuicSocketAddress kPeerAddress = |
| 97 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 98 | /*port=*/12345); |
| 99 | const QuicSocketAddress kSelfAddress = |
| 100 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 101 | /*port=*/443); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 102 | const QuicSocketAddress kServerPreferredAddress = QuicSocketAddress( |
| 103 | []() { |
| 104 | QuicIpAddress address; |
| 105 | address.FromString("2604:31c0::"); |
| 106 | return address; |
| 107 | }(), |
| 108 | /*port=*/443); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 109 | |
| 110 | QuicStreamId GetNthClientInitiatedStreamId(int n, |
| 111 | QuicTransportVersion version) { |
| 112 | return QuicUtils::GetFirstBidirectionalStreamId(version, |
| 113 | Perspective::IS_CLIENT) + |
| 114 | n * 2; |
| 115 | } |
| 116 | |
| 117 | QuicLongHeaderType EncryptionlevelToLongHeaderType(EncryptionLevel level) { |
| 118 | switch (level) { |
| 119 | case ENCRYPTION_INITIAL: |
| 120 | return INITIAL; |
| 121 | case ENCRYPTION_HANDSHAKE: |
| 122 | return HANDSHAKE; |
| 123 | case ENCRYPTION_ZERO_RTT: |
| 124 | return ZERO_RTT_PROTECTED; |
| 125 | case ENCRYPTION_FORWARD_SECURE: |
| 126 | QUICHE_DCHECK(false); |
| 127 | return INVALID_PACKET_TYPE; |
| 128 | default: |
| 129 | QUICHE_DCHECK(false); |
| 130 | return INVALID_PACKET_TYPE; |
| 131 | } |
| 132 | } |
| 133 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 134 | // A TaggingEncrypterWithConfidentialityLimit is a TaggingEncrypter that allows |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 135 | // specifying the confidentiality limit on the maximum number of packets that |
| 136 | // may be encrypted per key phase in TLS+QUIC. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 137 | class TaggingEncrypterWithConfidentialityLimit : public TaggingEncrypter { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 138 | public: |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 139 | TaggingEncrypterWithConfidentialityLimit( |
| 140 | uint8_t tag, QuicPacketCount confidentiality_limit) |
| 141 | : TaggingEncrypter(tag), confidentiality_limit_(confidentiality_limit) {} |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 142 | |
| 143 | QuicPacketCount GetConfidentialityLimit() const override { |
| 144 | return confidentiality_limit_; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | QuicPacketCount confidentiality_limit_; |
| 149 | }; |
| 150 | |
| 151 | class StrictTaggingDecrypterWithIntegrityLimit : public StrictTaggingDecrypter { |
| 152 | public: |
| 153 | StrictTaggingDecrypterWithIntegrityLimit(uint8_t tag, |
| 154 | QuicPacketCount integrity_limit) |
| 155 | : StrictTaggingDecrypter(tag), integrity_limit_(integrity_limit) {} |
| 156 | |
| 157 | QuicPacketCount GetIntegrityLimit() const override { |
| 158 | return integrity_limit_; |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | QuicPacketCount integrity_limit_; |
| 163 | }; |
| 164 | |
| 165 | class TestConnectionHelper : public QuicConnectionHelperInterface { |
| 166 | public: |
| 167 | TestConnectionHelper(MockClock* clock, MockRandom* random_generator) |
| 168 | : clock_(clock), random_generator_(random_generator) { |
| 169 | clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 170 | } |
| 171 | TestConnectionHelper(const TestConnectionHelper&) = delete; |
| 172 | TestConnectionHelper& operator=(const TestConnectionHelper&) = delete; |
| 173 | |
| 174 | // QuicConnectionHelperInterface |
| 175 | const QuicClock* GetClock() const override { return clock_; } |
| 176 | |
| 177 | QuicRandom* GetRandomGenerator() override { return random_generator_; } |
| 178 | |
| 179 | quiche::QuicheBufferAllocator* GetStreamSendBufferAllocator() override { |
| 180 | return &buffer_allocator_; |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | MockClock* clock_; |
| 185 | MockRandom* random_generator_; |
| 186 | quiche::SimpleBufferAllocator buffer_allocator_; |
| 187 | }; |
| 188 | |
| 189 | class TestConnection : public QuicConnection { |
| 190 | public: |
| 191 | TestConnection(QuicConnectionId connection_id, |
| 192 | QuicSocketAddress initial_self_address, |
| 193 | QuicSocketAddress initial_peer_address, |
| 194 | TestConnectionHelper* helper, TestAlarmFactory* alarm_factory, |
| 195 | TestPacketWriter* writer, Perspective perspective, |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 196 | ParsedQuicVersion version, |
| 197 | ConnectionIdGeneratorInterface& generator) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 198 | : QuicConnection(connection_id, initial_self_address, |
| 199 | initial_peer_address, helper, alarm_factory, writer, |
| 200 | /* owns_writer= */ false, perspective, |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 201 | SupportedVersions(version), generator), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 202 | notifier_(nullptr) { |
| 203 | writer->set_perspective(perspective); |
| 204 | SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 205 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 206 | SetDataProducer(&producer_); |
| 207 | ON_CALL(*this, OnSerializedPacket(_)) |
| 208 | .WillByDefault([this](SerializedPacket packet) { |
| 209 | QuicConnection::OnSerializedPacket(std::move(packet)); |
| 210 | }); |
| 211 | } |
| 212 | TestConnection(const TestConnection&) = delete; |
| 213 | TestConnection& operator=(const TestConnection&) = delete; |
| 214 | |
| 215 | MOCK_METHOD(void, OnSerializedPacket, (SerializedPacket packet), (override)); |
| 216 | |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 217 | void OnEffectivePeerMigrationValidated(bool is_migration_linkable) override { |
| 218 | QuicConnection::OnEffectivePeerMigrationValidated(is_migration_linkable); |
| 219 | if (is_migration_linkable) { |
| 220 | num_linkable_client_migration_++; |
| 221 | } else { |
| 222 | num_unlinkable_client_migration_++; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | uint32_t num_unlinkable_client_migration() const { |
| 227 | return num_unlinkable_client_migration_; |
| 228 | } |
| 229 | |
| 230 | uint32_t num_linkable_client_migration() const { |
| 231 | return num_linkable_client_migration_; |
| 232 | } |
| 233 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 234 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 235 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
| 236 | } |
| 237 | |
| 238 | void SetLossAlgorithm(LossDetectionInterface* loss_algorithm) { |
| 239 | QuicConnectionPeer::SetLossAlgorithm(this, loss_algorithm); |
| 240 | } |
| 241 | |
| 242 | void SendPacket(EncryptionLevel /*level*/, uint64_t packet_number, |
| 243 | std::unique_ptr<QuicPacket> packet, |
| 244 | HasRetransmittableData retransmittable, bool has_ack, |
| 245 | bool has_pending_frames) { |
| 246 | ScopedPacketFlusher flusher(this); |
| 247 | char buffer[kMaxOutgoingPacketSize]; |
| 248 | size_t encrypted_length = |
| 249 | QuicConnectionPeer::GetFramer(this)->EncryptPayload( |
| 250 | ENCRYPTION_INITIAL, QuicPacketNumber(packet_number), *packet, |
| 251 | buffer, kMaxOutgoingPacketSize); |
| 252 | SerializedPacket serialized_packet( |
| 253 | QuicPacketNumber(packet_number), PACKET_4BYTE_PACKET_NUMBER, buffer, |
| 254 | encrypted_length, has_ack, has_pending_frames); |
| 255 | serialized_packet.peer_address = kPeerAddress; |
| 256 | if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
| 257 | serialized_packet.retransmittable_frames.push_back( |
| 258 | QuicFrame(QuicPingFrame())); |
| 259 | } |
| 260 | OnSerializedPacket(std::move(serialized_packet)); |
| 261 | } |
| 262 | |
| 263 | QuicConsumedData SaveAndSendStreamData(QuicStreamId id, |
| 264 | absl::string_view data, |
| 265 | QuicStreamOffset offset, |
| 266 | StreamSendingState state) { |
QUICHE team | ac0a808 | 2022-06-13 09:17:05 -0700 | [diff] [blame] | 267 | return SaveAndSendStreamData(id, data, offset, state, NOT_RETRANSMISSION); |
| 268 | } |
| 269 | |
| 270 | QuicConsumedData SaveAndSendStreamData(QuicStreamId id, |
| 271 | absl::string_view data, |
| 272 | QuicStreamOffset offset, |
| 273 | StreamSendingState state, |
| 274 | TransmissionType transmission_type) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 275 | ScopedPacketFlusher flusher(this); |
| 276 | producer_.SaveStreamData(id, data); |
| 277 | if (notifier_ != nullptr) { |
QUICHE team | ac0a808 | 2022-06-13 09:17:05 -0700 | [diff] [blame] | 278 | return notifier_->WriteOrBufferData(id, data.length(), state, |
| 279 | transmission_type); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 280 | } |
| 281 | return QuicConnection::SendStreamData(id, data.length(), offset, state); |
| 282 | } |
| 283 | |
| 284 | QuicConsumedData SendStreamDataWithString(QuicStreamId id, |
| 285 | absl::string_view data, |
| 286 | QuicStreamOffset offset, |
| 287 | StreamSendingState state) { |
| 288 | ScopedPacketFlusher flusher(this); |
| 289 | if (!QuicUtils::IsCryptoStreamId(transport_version(), id) && |
| 290 | this->encryption_level() == ENCRYPTION_INITIAL) { |
| 291 | this->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 292 | if (perspective() == Perspective::IS_CLIENT && !IsHandshakeComplete()) { |
| 293 | OnHandshakeComplete(); |
| 294 | } |
| 295 | if (version().SupportsAntiAmplificationLimit()) { |
| 296 | QuicConnectionPeer::SetAddressValidated(this); |
| 297 | } |
| 298 | } |
| 299 | return SaveAndSendStreamData(id, data, offset, state); |
| 300 | } |
| 301 | |
| 302 | QuicConsumedData SendApplicationDataAtLevel(EncryptionLevel encryption_level, |
| 303 | QuicStreamId id, |
| 304 | absl::string_view data, |
| 305 | QuicStreamOffset offset, |
| 306 | StreamSendingState state) { |
| 307 | ScopedPacketFlusher flusher(this); |
| 308 | QUICHE_DCHECK(encryption_level >= ENCRYPTION_ZERO_RTT); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 309 | SetEncrypter(encryption_level, |
| 310 | std::make_unique<TaggingEncrypter>(encryption_level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 311 | SetDefaultEncryptionLevel(encryption_level); |
| 312 | return SaveAndSendStreamData(id, data, offset, state); |
| 313 | } |
| 314 | |
| 315 | QuicConsumedData SendStreamData3() { |
| 316 | return SendStreamDataWithString( |
| 317 | GetNthClientInitiatedStreamId(1, transport_version()), "food", 0, |
| 318 | NO_FIN); |
| 319 | } |
| 320 | |
| 321 | QuicConsumedData SendStreamData5() { |
| 322 | return SendStreamDataWithString( |
| 323 | GetNthClientInitiatedStreamId(2, transport_version()), "food2", 0, |
| 324 | NO_FIN); |
| 325 | } |
| 326 | |
| 327 | // Ensures the connection can write stream data before writing. |
| 328 | QuicConsumedData EnsureWritableAndSendStreamData5() { |
| 329 | EXPECT_TRUE(CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 330 | return SendStreamData5(); |
| 331 | } |
| 332 | |
| 333 | // The crypto stream has special semantics so that it is not blocked by a |
| 334 | // congestion window limitation, and also so that it gets put into a separate |
| 335 | // packet (so that it is easier to reason about a crypto frame not being |
| 336 | // split needlessly across packet boundaries). As a result, we have separate |
| 337 | // tests for some cases for this stream. |
| 338 | QuicConsumedData SendCryptoStreamData() { |
| 339 | QuicStreamOffset offset = 0; |
| 340 | absl::string_view data("chlo"); |
| 341 | if (!QuicVersionUsesCryptoFrames(transport_version())) { |
| 342 | return SendCryptoDataWithString(data, offset); |
| 343 | } |
| 344 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, offset, data); |
| 345 | size_t bytes_written; |
| 346 | if (notifier_) { |
| 347 | bytes_written = |
| 348 | notifier_->WriteCryptoData(ENCRYPTION_INITIAL, data.length(), offset); |
| 349 | } else { |
| 350 | bytes_written = QuicConnection::SendCryptoData(ENCRYPTION_INITIAL, |
| 351 | data.length(), offset); |
| 352 | } |
| 353 | return QuicConsumedData(bytes_written, /*fin_consumed*/ false); |
| 354 | } |
| 355 | |
| 356 | QuicConsumedData SendCryptoDataWithString(absl::string_view data, |
| 357 | QuicStreamOffset offset) { |
| 358 | return SendCryptoDataWithString(data, offset, ENCRYPTION_INITIAL); |
| 359 | } |
| 360 | |
| 361 | QuicConsumedData SendCryptoDataWithString(absl::string_view data, |
| 362 | QuicStreamOffset offset, |
| 363 | EncryptionLevel encryption_level) { |
| 364 | if (!QuicVersionUsesCryptoFrames(transport_version())) { |
| 365 | return SendStreamDataWithString( |
| 366 | QuicUtils::GetCryptoStreamId(transport_version()), data, offset, |
| 367 | NO_FIN); |
| 368 | } |
| 369 | producer_.SaveCryptoData(encryption_level, offset, data); |
| 370 | size_t bytes_written; |
| 371 | if (notifier_) { |
| 372 | bytes_written = |
| 373 | notifier_->WriteCryptoData(encryption_level, data.length(), offset); |
| 374 | } else { |
| 375 | bytes_written = QuicConnection::SendCryptoData(encryption_level, |
| 376 | data.length(), offset); |
| 377 | } |
| 378 | return QuicConsumedData(bytes_written, /*fin_consumed*/ false); |
| 379 | } |
| 380 | |
| 381 | void set_version(ParsedQuicVersion version) { |
| 382 | QuicConnectionPeer::GetFramer(this)->set_version(version); |
| 383 | } |
| 384 | |
| 385 | void SetSupportedVersions(const ParsedQuicVersionVector& versions) { |
| 386 | QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions); |
| 387 | writer()->SetSupportedVersions(versions); |
| 388 | } |
| 389 | |
| 390 | // This should be called before setting customized encrypters/decrypters for |
| 391 | // connection and peer creator. |
| 392 | void set_perspective(Perspective perspective) { |
| 393 | writer()->set_perspective(perspective); |
| 394 | QuicConnectionPeer::ResetPeerIssuedConnectionIdManager(this); |
| 395 | QuicConnectionPeer::SetPerspective(this, perspective); |
| 396 | QuicSentPacketManagerPeer::SetPerspective( |
| 397 | QuicConnectionPeer::GetSentPacketManager(this), perspective); |
| 398 | QuicConnectionPeer::GetFramer(this)->SetInitialObfuscators( |
| 399 | TestConnectionId()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Enable path MTU discovery. Assumes that the test is performed from the |
| 403 | // server perspective and the higher value of MTU target is used. |
| 404 | void EnablePathMtuDiscovery(MockSendAlgorithm* send_algorithm) { |
| 405 | ASSERT_EQ(Perspective::IS_SERVER, perspective()); |
| 406 | |
| 407 | if (GetQuicReloadableFlag(quic_enable_mtu_discovery_at_server)) { |
| 408 | OnConfigNegotiated(); |
| 409 | } else { |
| 410 | QuicConfig config; |
| 411 | QuicTagVector connection_options; |
| 412 | connection_options.push_back(kMTUH); |
| 413 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 414 | EXPECT_CALL(*send_algorithm, SetFromConfig(_, _)); |
| 415 | SetFromConfig(config); |
| 416 | } |
| 417 | |
| 418 | // Normally, the pacing would be disabled in the test, but calling |
| 419 | // SetFromConfig enables it. Set nearly-infinite bandwidth to make the |
| 420 | // pacing algorithm work. |
| 421 | EXPECT_CALL(*send_algorithm, PacingRate(_)) |
| 422 | .WillRepeatedly(Return(QuicBandwidth::Infinite())); |
| 423 | } |
| 424 | |
| 425 | TestAlarmFactory::TestAlarm* GetAckAlarm() { |
| 426 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 427 | QuicConnectionPeer::GetAckAlarm(this)); |
| 428 | } |
| 429 | |
| 430 | TestAlarmFactory::TestAlarm* GetPingAlarm() { |
| 431 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 432 | QuicConnectionPeer::GetPingAlarm(this)); |
| 433 | } |
| 434 | |
| 435 | TestAlarmFactory::TestAlarm* GetRetransmissionAlarm() { |
| 436 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 437 | QuicConnectionPeer::GetRetransmissionAlarm(this)); |
| 438 | } |
| 439 | |
| 440 | TestAlarmFactory::TestAlarm* GetSendAlarm() { |
| 441 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 442 | QuicConnectionPeer::GetSendAlarm(this)); |
| 443 | } |
| 444 | |
| 445 | TestAlarmFactory::TestAlarm* GetTimeoutAlarm() { |
| 446 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 447 | QuicConnectionPeer::GetIdleNetworkDetectorAlarm(this)); |
| 448 | } |
| 449 | |
| 450 | TestAlarmFactory::TestAlarm* GetMtuDiscoveryAlarm() { |
| 451 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 452 | QuicConnectionPeer::GetMtuDiscoveryAlarm(this)); |
| 453 | } |
| 454 | |
| 455 | TestAlarmFactory::TestAlarm* GetProcessUndecryptablePacketsAlarm() { |
| 456 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 457 | QuicConnectionPeer::GetProcessUndecryptablePacketsAlarm(this)); |
| 458 | } |
| 459 | |
| 460 | TestAlarmFactory::TestAlarm* GetDiscardPreviousOneRttKeysAlarm() { |
| 461 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 462 | QuicConnectionPeer::GetDiscardPreviousOneRttKeysAlarm(this)); |
| 463 | } |
| 464 | |
| 465 | TestAlarmFactory::TestAlarm* GetDiscardZeroRttDecryptionKeysAlarm() { |
| 466 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 467 | QuicConnectionPeer::GetDiscardZeroRttDecryptionKeysAlarm(this)); |
| 468 | } |
| 469 | |
| 470 | TestAlarmFactory::TestAlarm* GetBlackholeDetectorAlarm() { |
| 471 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 472 | QuicConnectionPeer::GetBlackholeDetectorAlarm(this)); |
| 473 | } |
| 474 | |
| 475 | TestAlarmFactory::TestAlarm* GetRetirePeerIssuedConnectionIdAlarm() { |
| 476 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 477 | QuicConnectionPeer::GetRetirePeerIssuedConnectionIdAlarm(this)); |
| 478 | } |
| 479 | |
| 480 | TestAlarmFactory::TestAlarm* GetRetireSelfIssuedConnectionIdAlarm() { |
| 481 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 482 | QuicConnectionPeer::GetRetireSelfIssuedConnectionIdAlarm(this)); |
| 483 | } |
| 484 | |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 485 | TestAlarmFactory::TestAlarm* GetMultiPortProbingAlarm() { |
| 486 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 487 | QuicConnectionPeer::GetMultiPortProbingAlarm(this)); |
| 488 | } |
| 489 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 490 | void PathDegradingTimeout() { |
| 491 | QUICHE_DCHECK(PathDegradingDetectionInProgress()); |
| 492 | GetBlackholeDetectorAlarm()->Fire(); |
| 493 | } |
| 494 | |
| 495 | bool PathDegradingDetectionInProgress() { |
| 496 | return QuicConnectionPeer::GetPathDegradingDeadline(this).IsInitialized(); |
| 497 | } |
| 498 | |
| 499 | bool BlackholeDetectionInProgress() { |
| 500 | return QuicConnectionPeer::GetBlackholeDetectionDeadline(this) |
| 501 | .IsInitialized(); |
| 502 | } |
| 503 | |
| 504 | bool PathMtuReductionDetectionInProgress() { |
| 505 | return QuicConnectionPeer::GetPathMtuReductionDetectionDeadline(this) |
| 506 | .IsInitialized(); |
| 507 | } |
| 508 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 509 | QuicByteCount GetBytesInFlight() { |
| 510 | return QuicConnectionPeer::GetSentPacketManager(this)->GetBytesInFlight(); |
| 511 | } |
| 512 | |
| 513 | void set_notifier(SimpleSessionNotifier* notifier) { notifier_ = notifier; } |
| 514 | |
| 515 | void ReturnEffectivePeerAddressForNextPacket(const QuicSocketAddress& addr) { |
| 516 | next_effective_peer_addr_ = std::make_unique<QuicSocketAddress>(addr); |
| 517 | } |
| 518 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 519 | void SendOrQueuePacket(SerializedPacket packet) override { |
| 520 | QuicConnection::SendOrQueuePacket(std::move(packet)); |
| 521 | self_address_on_default_path_while_sending_packet_ = self_address(); |
| 522 | } |
| 523 | |
| 524 | QuicSocketAddress self_address_on_default_path_while_sending_packet() { |
| 525 | return self_address_on_default_path_while_sending_packet_; |
| 526 | } |
| 527 | |
| 528 | SimpleDataProducer* producer() { return &producer_; } |
| 529 | |
| 530 | using QuicConnection::active_effective_peer_migration_type; |
| 531 | using QuicConnection::IsCurrentPacketConnectivityProbing; |
| 532 | using QuicConnection::SelectMutualVersion; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 533 | using QuicConnection::set_defer_send_in_response_to_packets; |
| 534 | |
| 535 | protected: |
| 536 | QuicSocketAddress GetEffectivePeerAddressFromCurrentPacket() const override { |
| 537 | if (next_effective_peer_addr_) { |
| 538 | return *std::move(next_effective_peer_addr_); |
| 539 | } |
| 540 | return QuicConnection::GetEffectivePeerAddressFromCurrentPacket(); |
| 541 | } |
| 542 | |
| 543 | private: |
| 544 | TestPacketWriter* writer() { |
| 545 | return static_cast<TestPacketWriter*>(QuicConnection::writer()); |
| 546 | } |
| 547 | |
| 548 | SimpleDataProducer producer_; |
| 549 | |
| 550 | SimpleSessionNotifier* notifier_; |
| 551 | |
| 552 | std::unique_ptr<QuicSocketAddress> next_effective_peer_addr_; |
| 553 | |
| 554 | QuicSocketAddress self_address_on_default_path_while_sending_packet_; |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 555 | |
| 556 | uint32_t num_unlinkable_client_migration_ = 0; |
| 557 | |
| 558 | uint32_t num_linkable_client_migration_ = 0; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | enum class AckResponse { kDefer, kImmediate }; |
| 562 | |
| 563 | // Run tests with combinations of {ParsedQuicVersion, AckResponse}. |
| 564 | struct TestParams { |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 565 | TestParams(ParsedQuicVersion version, AckResponse ack_response) |
| 566 | : version(version), ack_response(ack_response) {} |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 567 | |
| 568 | ParsedQuicVersion version; |
| 569 | AckResponse ack_response; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | // Used by ::testing::PrintToStringParamName(). |
| 573 | std::string PrintToString(const TestParams& p) { |
| 574 | return absl::StrCat( |
| 575 | ParsedQuicVersionToString(p.version), "_", |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 576 | (p.ack_response == AckResponse::kDefer ? "defer" : "immediate")); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Constructs various test permutations. |
| 580 | std::vector<TestParams> GetTestParams() { |
| 581 | QuicFlagSaver flags; |
| 582 | std::vector<TestParams> params; |
| 583 | ParsedQuicVersionVector all_supported_versions = AllSupportedVersions(); |
| 584 | for (size_t i = 0; i < all_supported_versions.size(); ++i) { |
| 585 | for (AckResponse ack_response : |
| 586 | {AckResponse::kDefer, AckResponse::kImmediate}) { |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 587 | params.push_back(TestParams(all_supported_versions[i], ack_response)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | return params; |
| 591 | } |
| 592 | |
| 593 | class QuicConnectionTest : public QuicTestWithParam<TestParams> { |
| 594 | public: |
| 595 | // For tests that do silent connection closes, no such packet is generated. In |
| 596 | // order to verify the contents of the OnConnectionClosed upcall, EXPECTs |
| 597 | // should invoke this method, saving the frame, and then the test can verify |
| 598 | // the contents. |
| 599 | void SaveConnectionCloseFrame(const QuicConnectionCloseFrame& frame, |
| 600 | ConnectionCloseSource /*source*/) { |
| 601 | saved_connection_close_frame_ = frame; |
| 602 | connection_close_frame_count_++; |
| 603 | } |
| 604 | |
| 605 | protected: |
| 606 | QuicConnectionTest() |
| 607 | : connection_id_(TestConnectionId()), |
| 608 | framer_(SupportedVersions(version()), QuicTime::Zero(), |
| 609 | Perspective::IS_CLIENT, connection_id_.length()), |
| 610 | send_algorithm_(new StrictMock<MockSendAlgorithm>), |
| 611 | loss_algorithm_(new MockLossAlgorithm()), |
| 612 | helper_(new TestConnectionHelper(&clock_, &random_generator_)), |
| 613 | alarm_factory_(new TestAlarmFactory()), |
| 614 | peer_framer_(SupportedVersions(version()), QuicTime::Zero(), |
| 615 | Perspective::IS_SERVER, connection_id_.length()), |
| 616 | peer_creator_(connection_id_, &peer_framer_, |
| 617 | /*delegate=*/nullptr), |
| 618 | writer_( |
| 619 | new TestPacketWriter(version(), &clock_, Perspective::IS_CLIENT)), |
| 620 | connection_(connection_id_, kSelfAddress, kPeerAddress, helper_.get(), |
| 621 | alarm_factory_.get(), writer_.get(), Perspective::IS_CLIENT, |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 622 | version(), connection_id_generator_), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 623 | creator_(QuicConnectionPeer::GetPacketCreator(&connection_)), |
| 624 | manager_(QuicConnectionPeer::GetSentPacketManager(&connection_)), |
| 625 | frame1_(0, false, 0, absl::string_view(data1)), |
| 626 | frame2_(0, false, 3, absl::string_view(data2)), |
| 627 | crypto_frame_(ENCRYPTION_INITIAL, 0, absl::string_view(data1)), |
| 628 | packet_number_length_(PACKET_4BYTE_PACKET_NUMBER), |
| 629 | connection_id_included_(CONNECTION_ID_PRESENT), |
| 630 | notifier_(&connection_), |
| 631 | connection_close_frame_count_(0) { |
| 632 | QUIC_DVLOG(2) << "QuicConnectionTest(" << PrintToString(GetParam()) << ")"; |
| 633 | connection_.set_defer_send_in_response_to_packets(GetParam().ack_response == |
| 634 | AckResponse::kDefer); |
| 635 | framer_.SetInitialObfuscators(TestConnectionId()); |
| 636 | connection_.InstallInitialCrypters(TestConnectionId()); |
| 637 | CrypterPair crypters; |
| 638 | CryptoUtils::CreateInitialObfuscators(Perspective::IS_SERVER, version(), |
| 639 | TestConnectionId(), &crypters); |
| 640 | peer_creator_.SetEncrypter(ENCRYPTION_INITIAL, |
| 641 | std::move(crypters.encrypter)); |
| 642 | if (version().KnowsWhichDecrypterToUse()) { |
| 643 | peer_framer_.InstallDecrypter(ENCRYPTION_INITIAL, |
| 644 | std::move(crypters.decrypter)); |
| 645 | } else { |
| 646 | peer_framer_.SetDecrypter(ENCRYPTION_INITIAL, |
| 647 | std::move(crypters.decrypter)); |
| 648 | } |
| 649 | for (EncryptionLevel level : |
| 650 | {ENCRYPTION_ZERO_RTT, ENCRYPTION_FORWARD_SECURE}) { |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 651 | peer_creator_.SetEncrypter(level, |
| 652 | std::make_unique<TaggingEncrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 653 | } |
| 654 | QuicFramerPeer::SetLastSerializedServerConnectionId( |
| 655 | QuicConnectionPeer::GetFramer(&connection_), connection_id_); |
| 656 | QuicFramerPeer::SetLastWrittenPacketNumberLength( |
| 657 | QuicConnectionPeer::GetFramer(&connection_), packet_number_length_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 658 | QuicStreamId stream_id; |
| 659 | if (QuicVersionUsesCryptoFrames(version().transport_version)) { |
| 660 | stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 661 | version().transport_version, Perspective::IS_CLIENT); |
| 662 | } else { |
| 663 | stream_id = QuicUtils::GetCryptoStreamId(version().transport_version); |
| 664 | } |
| 665 | frame1_.stream_id = stream_id; |
| 666 | frame2_.stream_id = stream_id; |
| 667 | connection_.set_visitor(&visitor_); |
| 668 | connection_.SetSessionNotifier(¬ifier_); |
| 669 | connection_.set_notifier(¬ifier_); |
| 670 | connection_.SetSendAlgorithm(send_algorithm_); |
| 671 | connection_.SetLossAlgorithm(loss_algorithm_.get()); |
| 672 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 673 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 674 | .Times(AnyNumber()); |
| 675 | EXPECT_CALL(*send_algorithm_, OnPacketNeutered(_)).Times(AnyNumber()); |
| 676 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 677 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 678 | EXPECT_CALL(*send_algorithm_, PacingRate(_)) |
| 679 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 680 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 681 | .Times(AnyNumber()) |
| 682 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 683 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)) |
| 684 | .Times(AnyNumber()); |
| 685 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 686 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 687 | EXPECT_CALL(*send_algorithm_, GetCongestionControlType()) |
| 688 | .Times(AnyNumber()); |
| 689 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 690 | EXPECT_CALL(*send_algorithm_, GetCongestionControlType()) |
| 691 | .Times(AnyNumber()); |
| 692 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).Times(AnyNumber()); |
| 693 | EXPECT_CALL(visitor_, OnPacketDecrypted(_)).Times(AnyNumber()); |
| 694 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 695 | .WillRepeatedly(Invoke(¬ifier_, &SimpleSessionNotifier::OnCanWrite)); |
| 696 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 697 | .WillRepeatedly(Return(false)); |
| 698 | EXPECT_CALL(visitor_, OnCongestionWindowChange(_)).Times(AnyNumber()); |
| 699 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(AnyNumber()); |
| 700 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
| 701 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()) |
| 702 | .Times(testing::AtMost(1)); |
| 703 | EXPECT_CALL(*loss_algorithm_, GetLossTimeout()) |
| 704 | .WillRepeatedly(Return(QuicTime::Zero())); |
| 705 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 706 | .Times(AnyNumber()); |
| 707 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 708 | .WillRepeatedly(Return(HANDSHAKE_START)); |
| 709 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 710 | connection_.InstallDecrypter( |
| 711 | ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 712 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
| 713 | } else { |
| 714 | connection_.SetAlternativeDecrypter( |
| 715 | ENCRYPTION_FORWARD_SECURE, |
| 716 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE), |
| 717 | false); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 718 | } |
| 719 | peer_creator_.SetDefaultPeerAddress(kSelfAddress); |
| 720 | } |
| 721 | |
| 722 | QuicConnectionTest(const QuicConnectionTest&) = delete; |
| 723 | QuicConnectionTest& operator=(const QuicConnectionTest&) = delete; |
| 724 | |
| 725 | ParsedQuicVersion version() { return GetParam().version; } |
| 726 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 727 | void SetClientConnectionId(const QuicConnectionId& client_connection_id) { |
| 728 | connection_.set_client_connection_id(client_connection_id); |
| 729 | writer_->framer()->framer()->SetExpectedClientConnectionIdLength( |
| 730 | client_connection_id.length()); |
| 731 | } |
| 732 | |
| 733 | void SetDecrypter(EncryptionLevel level, |
| 734 | std::unique_ptr<QuicDecrypter> decrypter) { |
| 735 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 736 | connection_.InstallDecrypter(level, std::move(decrypter)); |
| 737 | } else { |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 738 | connection_.SetAlternativeDecrypter(level, std::move(decrypter), false); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | void ProcessPacket(uint64_t number) { |
| 743 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 744 | ProcessDataPacket(number); |
| 745 | if (connection_.GetSendAlarm()->IsSet()) { |
| 746 | connection_.GetSendAlarm()->Fire(); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | void ProcessReceivedPacket(const QuicSocketAddress& self_address, |
| 751 | const QuicSocketAddress& peer_address, |
| 752 | const QuicReceivedPacket& packet) { |
| 753 | connection_.ProcessUdpPacket(self_address, peer_address, packet); |
| 754 | if (connection_.GetSendAlarm()->IsSet()) { |
| 755 | connection_.GetSendAlarm()->Fire(); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | QuicFrame MakeCryptoFrame() const { |
| 760 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 761 | return QuicFrame(new QuicCryptoFrame(crypto_frame_)); |
| 762 | } |
| 763 | return QuicFrame(QuicStreamFrame( |
| 764 | QuicUtils::GetCryptoStreamId(connection_.transport_version()), false, |
| 765 | 0u, absl::string_view())); |
| 766 | } |
| 767 | |
| 768 | void ProcessFramePacket(QuicFrame frame) { |
| 769 | ProcessFramePacketWithAddresses(frame, kSelfAddress, kPeerAddress, |
| 770 | ENCRYPTION_FORWARD_SECURE); |
| 771 | } |
| 772 | |
| 773 | void ProcessFramePacketWithAddresses(QuicFrame frame, |
| 774 | QuicSocketAddress self_address, |
| 775 | QuicSocketAddress peer_address, |
| 776 | EncryptionLevel level) { |
| 777 | QuicFrames frames; |
| 778 | frames.push_back(QuicFrame(frame)); |
| 779 | return ProcessFramesPacketWithAddresses(frames, self_address, peer_address, |
| 780 | level); |
| 781 | } |
| 782 | |
| 783 | std::unique_ptr<QuicReceivedPacket> ConstructPacket(QuicFrames frames, |
| 784 | EncryptionLevel level, |
| 785 | char* buffer, |
| 786 | size_t buffer_len) { |
| 787 | QUICHE_DCHECK(peer_framer_.HasEncrypterOfEncryptionLevel(level)); |
| 788 | peer_creator_.set_encryption_level(level); |
| 789 | QuicPacketCreatorPeer::SetSendVersionInPacket( |
| 790 | &peer_creator_, |
| 791 | level < ENCRYPTION_FORWARD_SECURE && |
| 792 | connection_.perspective() == Perspective::IS_SERVER); |
| 793 | |
| 794 | SerializedPacket serialized_packet = |
| 795 | QuicPacketCreatorPeer::SerializeAllFrames(&peer_creator_, frames, |
| 796 | buffer, buffer_len); |
| 797 | return std::make_unique<QuicReceivedPacket>( |
| 798 | serialized_packet.encrypted_buffer, serialized_packet.encrypted_length, |
| 799 | clock_.Now()); |
| 800 | } |
| 801 | |
| 802 | void ProcessFramesPacketWithAddresses(QuicFrames frames, |
| 803 | QuicSocketAddress self_address, |
| 804 | QuicSocketAddress peer_address, |
| 805 | EncryptionLevel level) { |
| 806 | char buffer[kMaxOutgoingPacketSize]; |
| 807 | connection_.ProcessUdpPacket( |
| 808 | self_address, peer_address, |
| 809 | *ConstructPacket(std::move(frames), level, buffer, |
| 810 | kMaxOutgoingPacketSize)); |
| 811 | if (connection_.GetSendAlarm()->IsSet()) { |
| 812 | connection_.GetSendAlarm()->Fire(); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // Bypassing the packet creator is unrealistic, but allows us to process |
| 817 | // packets the QuicPacketCreator won't allow us to create. |
| 818 | void ForceProcessFramePacket(QuicFrame frame) { |
| 819 | QuicFrames frames; |
| 820 | frames.push_back(QuicFrame(frame)); |
| 821 | bool send_version = connection_.perspective() == Perspective::IS_SERVER; |
| 822 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 823 | send_version = true; |
| 824 | } |
| 825 | QuicPacketCreatorPeer::SetSendVersionInPacket(&peer_creator_, send_version); |
| 826 | QuicPacketHeader header; |
| 827 | QuicPacketCreatorPeer::FillPacketHeader(&peer_creator_, &header); |
| 828 | char encrypted_buffer[kMaxOutgoingPacketSize]; |
| 829 | size_t length = peer_framer_.BuildDataPacket( |
| 830 | header, frames, encrypted_buffer, kMaxOutgoingPacketSize, |
| 831 | ENCRYPTION_INITIAL); |
| 832 | QUICHE_DCHECK_GT(length, 0u); |
| 833 | |
| 834 | const size_t encrypted_length = peer_framer_.EncryptInPlace( |
| 835 | ENCRYPTION_INITIAL, header.packet_number, |
| 836 | GetStartOfEncryptedData(peer_framer_.version().transport_version, |
| 837 | header), |
| 838 | length, kMaxOutgoingPacketSize, encrypted_buffer); |
| 839 | QUICHE_DCHECK_GT(encrypted_length, 0u); |
| 840 | |
| 841 | connection_.ProcessUdpPacket( |
| 842 | kSelfAddress, kPeerAddress, |
| 843 | QuicReceivedPacket(encrypted_buffer, encrypted_length, clock_.Now())); |
| 844 | } |
| 845 | |
| 846 | size_t ProcessFramePacketAtLevel(uint64_t number, QuicFrame frame, |
| 847 | EncryptionLevel level) { |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 848 | return ProcessFramePacketAtLevelWithEcn(number, frame, level, ECN_NOT_ECT); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 849 | } |
| 850 | |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 851 | size_t ProcessFramePacketAtLevelWithEcn(uint64_t number, QuicFrame frame, |
| 852 | EncryptionLevel level, |
| 853 | QuicEcnCodepoint ecn_codepoint) { |
| 854 | QuicFrames frames; |
| 855 | frames.push_back(frame); |
| 856 | return ProcessFramesPacketAtLevelWithEcn(number, frames, level, |
| 857 | ecn_codepoint); |
| 858 | } |
| 859 | |
| 860 | size_t ProcessFramesPacketAtLevel(uint64_t number, QuicFrames frames, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 861 | EncryptionLevel level) { |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 862 | return ProcessFramesPacketAtLevelWithEcn(number, frames, level, |
| 863 | ECN_NOT_ECT); |
| 864 | } |
| 865 | |
| 866 | size_t ProcessFramesPacketAtLevelWithEcn(uint64_t number, |
| 867 | const QuicFrames& frames, |
| 868 | EncryptionLevel level, |
| 869 | QuicEcnCodepoint ecn_codepoint) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 870 | QuicPacketHeader header = ConstructPacketHeader(number, level); |
| 871 | // Set the correct encryption level and encrypter on peer_creator and |
| 872 | // peer_framer, respectively. |
| 873 | peer_creator_.set_encryption_level(level); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 874 | if (level > ENCRYPTION_INITIAL) { |
| 875 | peer_framer_.SetEncrypter(level, |
| 876 | std::make_unique<TaggingEncrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 877 | // Set the corresponding decrypter. |
| 878 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 879 | connection_.InstallDecrypter( |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 880 | level, std::make_unique<StrictTaggingDecrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 881 | } else { |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 882 | connection_.SetAlternativeDecrypter( |
| 883 | level, std::make_unique<StrictTaggingDecrypter>(level), false); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 887 | |
| 888 | char buffer[kMaxOutgoingPacketSize]; |
| 889 | size_t encrypted_length = |
| 890 | peer_framer_.EncryptPayload(level, QuicPacketNumber(number), *packet, |
| 891 | buffer, kMaxOutgoingPacketSize); |
| 892 | connection_.ProcessUdpPacket( |
| 893 | kSelfAddress, kPeerAddress, |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 894 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false, 0, |
| 895 | true, nullptr, 0, false, ecn_codepoint)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 896 | if (connection_.GetSendAlarm()->IsSet()) { |
| 897 | connection_.GetSendAlarm()->Fire(); |
| 898 | } |
| 899 | return encrypted_length; |
| 900 | } |
| 901 | |
| 902 | struct PacketInfo { |
| 903 | PacketInfo(uint64_t packet_number, QuicFrames frames, EncryptionLevel level) |
| 904 | : packet_number(packet_number), frames(frames), level(level) {} |
| 905 | |
| 906 | uint64_t packet_number; |
| 907 | QuicFrames frames; |
| 908 | EncryptionLevel level; |
| 909 | }; |
| 910 | |
| 911 | size_t ProcessCoalescedPacket(std::vector<PacketInfo> packets) { |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 912 | return ProcessCoalescedPacket(packets, ECN_NOT_ECT); |
| 913 | } |
| 914 | |
| 915 | size_t ProcessCoalescedPacket(std::vector<PacketInfo> packets, |
| 916 | QuicEcnCodepoint ecn_codepoint) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 917 | char coalesced_buffer[kMaxOutgoingPacketSize]; |
| 918 | size_t coalesced_size = 0; |
| 919 | bool contains_initial = false; |
| 920 | for (const auto& packet : packets) { |
| 921 | QuicPacketHeader header = |
| 922 | ConstructPacketHeader(packet.packet_number, packet.level); |
| 923 | // Set the correct encryption level and encrypter on peer_creator and |
| 924 | // peer_framer, respectively. |
| 925 | peer_creator_.set_encryption_level(packet.level); |
| 926 | if (packet.level == ENCRYPTION_INITIAL) { |
| 927 | contains_initial = true; |
| 928 | } |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 929 | EncryptionLevel level = |
| 930 | QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_); |
| 931 | if (level > ENCRYPTION_INITIAL) { |
| 932 | peer_framer_.SetEncrypter(level, |
| 933 | std::make_unique<TaggingEncrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 934 | // Set the corresponding decrypter. |
| 935 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 936 | connection_.InstallDecrypter( |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 937 | level, std::make_unique<StrictTaggingDecrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 938 | } else { |
| 939 | connection_.SetDecrypter( |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 940 | level, std::make_unique<StrictTaggingDecrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | std::unique_ptr<QuicPacket> constructed_packet( |
| 944 | ConstructPacket(header, packet.frames)); |
| 945 | |
| 946 | char buffer[kMaxOutgoingPacketSize]; |
| 947 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 948 | packet.level, QuicPacketNumber(packet.packet_number), |
| 949 | *constructed_packet, buffer, kMaxOutgoingPacketSize); |
| 950 | QUICHE_DCHECK_LE(coalesced_size + encrypted_length, |
| 951 | kMaxOutgoingPacketSize); |
| 952 | memcpy(coalesced_buffer + coalesced_size, buffer, encrypted_length); |
| 953 | coalesced_size += encrypted_length; |
| 954 | } |
| 955 | if (contains_initial) { |
| 956 | // Padded coalesced packet to full if it contains initial packet. |
| 957 | memset(coalesced_buffer + coalesced_size, '0', |
| 958 | kMaxOutgoingPacketSize - coalesced_size); |
| 959 | } |
| 960 | connection_.ProcessUdpPacket( |
| 961 | kSelfAddress, kPeerAddress, |
| 962 | QuicReceivedPacket(coalesced_buffer, coalesced_size, clock_.Now(), |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 963 | false, 0, true, nullptr, 0, false, ecn_codepoint)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 964 | if (connection_.GetSendAlarm()->IsSet()) { |
| 965 | connection_.GetSendAlarm()->Fire(); |
| 966 | } |
| 967 | return coalesced_size; |
| 968 | } |
| 969 | |
| 970 | size_t ProcessDataPacket(uint64_t number) { |
| 971 | return ProcessDataPacketAtLevel(number, false, ENCRYPTION_FORWARD_SECURE); |
| 972 | } |
| 973 | |
| 974 | size_t ProcessDataPacket(QuicPacketNumber packet_number) { |
| 975 | return ProcessDataPacketAtLevel(packet_number, false, |
| 976 | ENCRYPTION_FORWARD_SECURE); |
| 977 | } |
| 978 | |
| 979 | size_t ProcessDataPacketAtLevel(QuicPacketNumber packet_number, |
| 980 | bool has_stop_waiting, |
| 981 | EncryptionLevel level) { |
| 982 | return ProcessDataPacketAtLevel(packet_number.ToUint64(), has_stop_waiting, |
| 983 | level); |
| 984 | } |
| 985 | |
| 986 | size_t ProcessCryptoPacketAtLevel(uint64_t number, EncryptionLevel level) { |
| 987 | QuicPacketHeader header = ConstructPacketHeader(number, level); |
| 988 | QuicFrames frames; |
| 989 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 990 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 991 | } else { |
| 992 | frames.push_back(QuicFrame(frame1_)); |
| 993 | } |
| 994 | if (level == ENCRYPTION_INITIAL) { |
| 995 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 996 | } |
| 997 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 998 | char buffer[kMaxOutgoingPacketSize]; |
| 999 | peer_creator_.set_encryption_level(level); |
| 1000 | size_t encrypted_length = |
| 1001 | peer_framer_.EncryptPayload(level, QuicPacketNumber(number), *packet, |
| 1002 | buffer, kMaxOutgoingPacketSize); |
| 1003 | connection_.ProcessUdpPacket( |
| 1004 | kSelfAddress, kPeerAddress, |
| 1005 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 1006 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1007 | connection_.GetSendAlarm()->Fire(); |
| 1008 | } |
| 1009 | return encrypted_length; |
| 1010 | } |
| 1011 | |
| 1012 | size_t ProcessDataPacketAtLevel(uint64_t number, bool has_stop_waiting, |
| 1013 | EncryptionLevel level) { |
| 1014 | std::unique_ptr<QuicPacket> packet( |
| 1015 | ConstructDataPacket(number, has_stop_waiting, level)); |
| 1016 | char buffer[kMaxOutgoingPacketSize]; |
| 1017 | peer_creator_.set_encryption_level(level); |
| 1018 | size_t encrypted_length = |
| 1019 | peer_framer_.EncryptPayload(level, QuicPacketNumber(number), *packet, |
| 1020 | buffer, kMaxOutgoingPacketSize); |
| 1021 | connection_.ProcessUdpPacket( |
| 1022 | kSelfAddress, kPeerAddress, |
| 1023 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 1024 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1025 | connection_.GetSendAlarm()->Fire(); |
| 1026 | } |
| 1027 | return encrypted_length; |
| 1028 | } |
| 1029 | |
| 1030 | void ProcessClosePacket(uint64_t number) { |
| 1031 | std::unique_ptr<QuicPacket> packet(ConstructClosePacket(number)); |
| 1032 | char buffer[kMaxOutgoingPacketSize]; |
| 1033 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 1034 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(number), *packet, buffer, |
| 1035 | kMaxOutgoingPacketSize); |
| 1036 | connection_.ProcessUdpPacket( |
| 1037 | kSelfAddress, kPeerAddress, |
| 1038 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
| 1039 | } |
| 1040 | |
| 1041 | QuicByteCount SendStreamDataToPeer(QuicStreamId id, absl::string_view data, |
| 1042 | QuicStreamOffset offset, |
| 1043 | StreamSendingState state, |
| 1044 | QuicPacketNumber* last_packet) { |
QUICHE team | 24a2385 | 2022-10-31 18:03:52 -0700 | [diff] [blame] | 1045 | QuicByteCount packet_size = 0; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1046 | // Save the last packet's size. |
| 1047 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1048 | .Times(AnyNumber()) |
| 1049 | .WillRepeatedly(SaveArg<3>(&packet_size)); |
| 1050 | connection_.SendStreamDataWithString(id, data, offset, state); |
| 1051 | if (last_packet != nullptr) { |
| 1052 | *last_packet = creator_->packet_number(); |
| 1053 | } |
| 1054 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1055 | .Times(AnyNumber()); |
| 1056 | return packet_size; |
| 1057 | } |
| 1058 | |
| 1059 | void SendAckPacketToPeer() { |
| 1060 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 1061 | { |
| 1062 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 1063 | connection_.SendAck(); |
| 1064 | } |
| 1065 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1066 | .Times(AnyNumber()); |
| 1067 | } |
| 1068 | |
| 1069 | void SendRstStream(QuicStreamId id, QuicRstStreamErrorCode error, |
| 1070 | QuicStreamOffset bytes_written) { |
| 1071 | notifier_.WriteOrBufferRstStream(id, error, bytes_written); |
| 1072 | connection_.OnStreamReset(id, error); |
| 1073 | } |
| 1074 | |
| 1075 | void SendPing() { notifier_.WriteOrBufferPing(); } |
| 1076 | |
| 1077 | MessageStatus SendMessage(absl::string_view message) { |
| 1078 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1079 | quiche::QuicheMemSlice slice(quiche::QuicheBuffer::Copy( |
| 1080 | connection_.helper()->GetStreamSendBufferAllocator(), message)); |
| 1081 | return connection_.SendMessage(1, absl::MakeSpan(&slice, 1), false); |
| 1082 | } |
| 1083 | |
| 1084 | void ProcessAckPacket(uint64_t packet_number, QuicAckFrame* frame) { |
| 1085 | if (packet_number > 1) { |
| 1086 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, packet_number - 1); |
| 1087 | } else { |
| 1088 | QuicPacketCreatorPeer::ClearPacketNumber(&peer_creator_); |
| 1089 | } |
| 1090 | ProcessFramePacket(QuicFrame(frame)); |
| 1091 | } |
| 1092 | |
| 1093 | void ProcessAckPacket(QuicAckFrame* frame) { |
| 1094 | ProcessFramePacket(QuicFrame(frame)); |
| 1095 | } |
| 1096 | |
| 1097 | void ProcessStopWaitingPacket(QuicStopWaitingFrame frame) { |
| 1098 | ProcessFramePacket(QuicFrame(frame)); |
| 1099 | } |
| 1100 | |
| 1101 | size_t ProcessStopWaitingPacketAtLevel(uint64_t number, |
| 1102 | QuicStopWaitingFrame frame, |
| 1103 | EncryptionLevel /*level*/) { |
| 1104 | return ProcessFramePacketAtLevel(number, QuicFrame(frame), |
| 1105 | ENCRYPTION_ZERO_RTT); |
| 1106 | } |
| 1107 | |
| 1108 | void ProcessGoAwayPacket(QuicGoAwayFrame* frame) { |
| 1109 | ProcessFramePacket(QuicFrame(frame)); |
| 1110 | } |
| 1111 | |
| 1112 | bool IsMissing(uint64_t number) { |
| 1113 | return IsAwaitingPacket(connection_.ack_frame(), QuicPacketNumber(number), |
| 1114 | QuicPacketNumber()); |
| 1115 | } |
| 1116 | |
| 1117 | std::unique_ptr<QuicPacket> ConstructPacket(const QuicPacketHeader& header, |
| 1118 | const QuicFrames& frames) { |
| 1119 | auto packet = BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 1120 | EXPECT_NE(nullptr, packet.get()); |
| 1121 | return packet; |
| 1122 | } |
| 1123 | |
| 1124 | QuicPacketHeader ConstructPacketHeader(uint64_t number, |
| 1125 | EncryptionLevel level) { |
| 1126 | QuicPacketHeader header; |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 1127 | if (level < ENCRYPTION_FORWARD_SECURE) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1128 | // Set long header type accordingly. |
| 1129 | header.version_flag = true; |
| 1130 | header.form = IETF_QUIC_LONG_HEADER_PACKET; |
| 1131 | header.long_packet_type = EncryptionlevelToLongHeaderType(level); |
| 1132 | if (QuicVersionHasLongHeaderLengths( |
| 1133 | peer_framer_.version().transport_version)) { |
dschinazi | 35c0ff7 | 2022-08-16 12:10:06 -0700 | [diff] [blame] | 1134 | header.length_length = quiche::VARIABLE_LENGTH_INTEGER_LENGTH_2; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1135 | if (header.long_packet_type == INITIAL) { |
dschinazi | 35c0ff7 | 2022-08-16 12:10:06 -0700 | [diff] [blame] | 1136 | header.retry_token_length_length = |
| 1137 | quiche::VARIABLE_LENGTH_INTEGER_LENGTH_1; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | // Set connection_id to peer's in memory representation as this data packet |
| 1142 | // is created by peer_framer. |
| 1143 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
| 1144 | header.source_connection_id = connection_id_; |
| 1145 | header.source_connection_id_included = connection_id_included_; |
| 1146 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 1147 | } else { |
| 1148 | header.destination_connection_id = connection_id_; |
| 1149 | header.destination_connection_id_included = connection_id_included_; |
| 1150 | } |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 1151 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1152 | if (!connection_.client_connection_id().IsEmpty()) { |
| 1153 | header.destination_connection_id = connection_.client_connection_id(); |
| 1154 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 1155 | } else { |
| 1156 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 1157 | } |
| 1158 | if (header.version_flag) { |
| 1159 | header.source_connection_id = connection_id_; |
| 1160 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 1161 | if (GetParam().version.handshake_protocol == PROTOCOL_QUIC_CRYPTO && |
| 1162 | header.long_packet_type == ZERO_RTT_PROTECTED) { |
| 1163 | header.nonce = &kTestDiversificationNonce; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | header.packet_number_length = packet_number_length_; |
| 1168 | header.packet_number = QuicPacketNumber(number); |
| 1169 | return header; |
| 1170 | } |
| 1171 | |
| 1172 | std::unique_ptr<QuicPacket> ConstructDataPacket(uint64_t number, |
| 1173 | bool has_stop_waiting, |
| 1174 | EncryptionLevel level) { |
| 1175 | QuicPacketHeader header = ConstructPacketHeader(number, level); |
| 1176 | QuicFrames frames; |
| 1177 | if (VersionHasIetfQuicFrames(version().transport_version) && |
| 1178 | (level == ENCRYPTION_INITIAL || level == ENCRYPTION_HANDSHAKE)) { |
| 1179 | frames.push_back(QuicFrame(QuicPingFrame())); |
| 1180 | frames.push_back(QuicFrame(QuicPaddingFrame(100))); |
| 1181 | } else { |
| 1182 | frames.push_back(QuicFrame(frame1_)); |
| 1183 | if (has_stop_waiting) { |
| 1184 | frames.push_back(QuicFrame(stop_waiting_)); |
| 1185 | } |
| 1186 | } |
| 1187 | return ConstructPacket(header, frames); |
| 1188 | } |
| 1189 | |
| 1190 | std::unique_ptr<SerializedPacket> ConstructProbingPacket() { |
| 1191 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1192 | if (VersionHasIetfQuicFrames(version().transport_version)) { |
| 1193 | QuicPathFrameBuffer payload = { |
| 1194 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 1195 | return QuicPacketCreatorPeer:: |
| 1196 | SerializePathChallengeConnectivityProbingPacket(&peer_creator_, |
| 1197 | payload); |
| 1198 | } |
| 1199 | return QuicPacketCreatorPeer::SerializeConnectivityProbingPacket( |
| 1200 | &peer_creator_); |
| 1201 | } |
| 1202 | |
| 1203 | std::unique_ptr<QuicPacket> ConstructClosePacket(uint64_t number) { |
| 1204 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1205 | QuicPacketHeader header; |
| 1206 | // Set connection_id to peer's in memory representation as this connection |
| 1207 | // close packet is created by peer_framer. |
| 1208 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
| 1209 | header.source_connection_id = connection_id_; |
| 1210 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1211 | } else { |
| 1212 | header.destination_connection_id = connection_id_; |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 1213 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | header.packet_number = QuicPacketNumber(number); |
| 1217 | |
| 1218 | QuicErrorCode kQuicErrorCode = QUIC_PEER_GOING_AWAY; |
| 1219 | QuicConnectionCloseFrame qccf(peer_framer_.transport_version(), |
| 1220 | kQuicErrorCode, NO_IETF_QUIC_ERROR, "", |
| 1221 | /*transport_close_frame_type=*/0); |
| 1222 | QuicFrames frames; |
| 1223 | frames.push_back(QuicFrame(&qccf)); |
| 1224 | return ConstructPacket(header, frames); |
| 1225 | } |
| 1226 | |
| 1227 | QuicTime::Delta DefaultRetransmissionTime() { |
| 1228 | return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); |
| 1229 | } |
| 1230 | |
| 1231 | QuicTime::Delta DefaultDelayedAckTime() { |
| 1232 | return QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs); |
| 1233 | } |
| 1234 | |
| 1235 | const QuicStopWaitingFrame InitStopWaitingFrame(uint64_t least_unacked) { |
| 1236 | QuicStopWaitingFrame frame; |
| 1237 | frame.least_unacked = QuicPacketNumber(least_unacked); |
| 1238 | return frame; |
| 1239 | } |
| 1240 | |
| 1241 | // Construct a ack_frame that acks all packet numbers between 1 and |
| 1242 | // |largest_acked|, except |missing|. |
| 1243 | // REQUIRES: 1 <= |missing| < |largest_acked| |
| 1244 | QuicAckFrame ConstructAckFrame(uint64_t largest_acked, uint64_t missing) { |
| 1245 | return ConstructAckFrame(QuicPacketNumber(largest_acked), |
| 1246 | QuicPacketNumber(missing)); |
| 1247 | } |
| 1248 | |
| 1249 | QuicAckFrame ConstructAckFrame(QuicPacketNumber largest_acked, |
| 1250 | QuicPacketNumber missing) { |
| 1251 | if (missing == QuicPacketNumber(1)) { |
| 1252 | return InitAckFrame({{missing + 1, largest_acked + 1}}); |
| 1253 | } |
| 1254 | return InitAckFrame( |
| 1255 | {{QuicPacketNumber(1), missing}, {missing + 1, largest_acked + 1}}); |
| 1256 | } |
| 1257 | |
| 1258 | // Undo nacking a packet within the frame. |
| 1259 | void AckPacket(QuicPacketNumber arrived, QuicAckFrame* frame) { |
| 1260 | EXPECT_FALSE(frame->packets.Contains(arrived)); |
| 1261 | frame->packets.Add(arrived); |
| 1262 | } |
| 1263 | |
| 1264 | void TriggerConnectionClose() { |
| 1265 | // Send an erroneous packet to close the connection. |
| 1266 | EXPECT_CALL(visitor_, |
| 1267 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 1268 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 1269 | |
| 1270 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 1271 | // Triggers a connection by receiving ACK of unsent packet. |
| 1272 | QuicAckFrame frame = InitAckFrame(10000); |
| 1273 | ProcessAckPacket(1, &frame); |
| 1274 | EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == |
| 1275 | nullptr); |
| 1276 | EXPECT_EQ(1, connection_close_frame_count_); |
| 1277 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 1278 | IsError(QUIC_INVALID_ACK_DATA)); |
| 1279 | } |
| 1280 | |
| 1281 | void BlockOnNextWrite() { |
| 1282 | writer_->BlockOnNextWrite(); |
| 1283 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 1284 | } |
| 1285 | |
| 1286 | void SimulateNextPacketTooLarge() { writer_->SimulateNextPacketTooLarge(); } |
| 1287 | |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 1288 | void ExpectNextPacketUnprocessable() { |
| 1289 | writer_->ExpectNextPacketUnprocessable(); |
| 1290 | } |
| 1291 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1292 | void AlwaysGetPacketTooLarge() { writer_->AlwaysGetPacketTooLarge(); } |
| 1293 | |
| 1294 | void SetWritePauseTimeDelta(QuicTime::Delta delta) { |
| 1295 | writer_->SetWritePauseTimeDelta(delta); |
| 1296 | } |
| 1297 | |
| 1298 | void CongestionBlockWrites() { |
| 1299 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 1300 | .WillRepeatedly(testing::Return(false)); |
| 1301 | } |
| 1302 | |
| 1303 | void CongestionUnblockWrites() { |
| 1304 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 1305 | .WillRepeatedly(testing::Return(true)); |
| 1306 | } |
| 1307 | |
| 1308 | void set_perspective(Perspective perspective) { |
| 1309 | connection_.set_perspective(perspective); |
| 1310 | if (perspective == Perspective::IS_SERVER) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1311 | connection_.set_can_truncate_connection_ids(true); |
| 1312 | QuicConnectionPeer::SetNegotiatedVersion(&connection_); |
| 1313 | connection_.OnSuccessfulVersionNegotiation(); |
| 1314 | } |
| 1315 | QuicFramerPeer::SetPerspective(&peer_framer_, |
| 1316 | QuicUtils::InvertPerspective(perspective)); |
| 1317 | peer_framer_.SetInitialObfuscators(TestConnectionId()); |
| 1318 | for (EncryptionLevel level : {ENCRYPTION_ZERO_RTT, ENCRYPTION_HANDSHAKE, |
| 1319 | ENCRYPTION_FORWARD_SECURE}) { |
| 1320 | if (peer_framer_.HasEncrypterOfEncryptionLevel(level)) { |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 1321 | peer_creator_.SetEncrypter(level, |
| 1322 | std::make_unique<TaggingEncrypter>(level)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | void set_packets_between_probes_base( |
| 1328 | const QuicPacketCount packets_between_probes_base) { |
| 1329 | QuicConnectionPeer::ReInitializeMtuDiscoverer( |
| 1330 | &connection_, packets_between_probes_base, |
| 1331 | QuicPacketNumber(packets_between_probes_base)); |
| 1332 | } |
| 1333 | |
| 1334 | bool IsDefaultTestConfiguration() { |
| 1335 | TestParams p = GetParam(); |
| 1336 | return p.ack_response == AckResponse::kImmediate && |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 1337 | p.version == AllSupportedVersions()[0]; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | void TestConnectionCloseQuicErrorCode(QuicErrorCode expected_code) { |
| 1341 | // Not strictly needed for this test, but is commonly done. |
| 1342 | EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == |
| 1343 | nullptr); |
| 1344 | const std::vector<QuicConnectionCloseFrame>& connection_close_frames = |
| 1345 | writer_->connection_close_frames(); |
| 1346 | ASSERT_EQ(1u, connection_close_frames.size()); |
| 1347 | |
| 1348 | EXPECT_THAT(connection_close_frames[0].quic_error_code, |
| 1349 | IsError(expected_code)); |
| 1350 | |
| 1351 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 1352 | EXPECT_THAT(connection_close_frames[0].wire_error_code, |
| 1353 | IsError(expected_code)); |
| 1354 | EXPECT_EQ(GOOGLE_QUIC_CONNECTION_CLOSE, |
| 1355 | connection_close_frames[0].close_type); |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | QuicErrorCodeToIetfMapping mapping = |
| 1360 | QuicErrorCodeToTransportErrorCode(expected_code); |
| 1361 | |
| 1362 | if (mapping.is_transport_close) { |
| 1363 | // This Google QUIC Error Code maps to a transport close, |
| 1364 | EXPECT_EQ(IETF_QUIC_TRANSPORT_CONNECTION_CLOSE, |
| 1365 | connection_close_frames[0].close_type); |
| 1366 | } else { |
| 1367 | // This maps to an application close. |
| 1368 | EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE, |
| 1369 | connection_close_frames[0].close_type); |
| 1370 | } |
| 1371 | EXPECT_EQ(mapping.error_code, connection_close_frames[0].wire_error_code); |
| 1372 | } |
| 1373 | |
| 1374 | void MtuDiscoveryTestInit() { |
| 1375 | set_perspective(Perspective::IS_SERVER); |
| 1376 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1377 | if (version().SupportsAntiAmplificationLimit()) { |
| 1378 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1379 | } |
| 1380 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1381 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1382 | // Prevent packets from being coalesced. |
| 1383 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1384 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1385 | EXPECT_TRUE(connection_.connected()); |
| 1386 | } |
| 1387 | |
| 1388 | void PathProbeTestInit(Perspective perspective, |
| 1389 | bool receive_new_server_connection_id = true) { |
| 1390 | set_perspective(perspective); |
| 1391 | connection_.CreateConnectionIdManager(); |
| 1392 | EXPECT_EQ(connection_.perspective(), perspective); |
| 1393 | if (perspective == Perspective::IS_SERVER) { |
| 1394 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1395 | } |
| 1396 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1397 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1398 | // Discard INITIAL key. |
| 1399 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 1400 | connection_.NeuterUnencryptedPackets(); |
| 1401 | // Prevent packets from being coalesced. |
| 1402 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1403 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1404 | if (version().SupportsAntiAmplificationLimit() && |
| 1405 | perspective == Perspective::IS_SERVER) { |
| 1406 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1407 | } |
| 1408 | // Clear direct_peer_address. |
| 1409 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1410 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1411 | // this test. |
| 1412 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1413 | QuicSocketAddress()); |
| 1414 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1415 | |
| 1416 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1417 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 1418 | } else { |
| 1419 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 1420 | } |
| 1421 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 1422 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1423 | kPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 1424 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1425 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1426 | if (perspective == Perspective::IS_CLIENT && |
| 1427 | receive_new_server_connection_id && version().HasIetfQuicFrames()) { |
| 1428 | QuicNewConnectionIdFrame frame; |
| 1429 | frame.connection_id = TestConnectionId(1234); |
| 1430 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 1431 | frame.stateless_reset_token = |
| 1432 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 1433 | frame.retire_prior_to = 0u; |
| 1434 | frame.sequence_number = 1u; |
| 1435 | connection_.OnNewConnectionIdFrame(frame); |
| 1436 | } |
| 1437 | } |
| 1438 | |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 1439 | void ServerHandlePreferredAddressInit() { |
| 1440 | ASSERT_TRUE(GetParam().version.HasIetfQuicFrames()); |
| 1441 | set_perspective(Perspective::IS_SERVER); |
| 1442 | connection_.CreateConnectionIdManager(); |
| 1443 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1444 | SetQuicReloadableFlag(quic_connection_migration_use_new_cid_v2, true); |
fayang | 1f578d3 | 2023-03-20 11:36:15 -0700 | [diff] [blame] | 1445 | SetQuicReloadableFlag(quic_use_received_client_addresses_cache, true); |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 1446 | EXPECT_CALL(visitor_, AllowSelfAddressChange()) |
| 1447 | .WillRepeatedly(Return(true)); |
| 1448 | |
| 1449 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1450 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1451 | // Discard INITIAL key. |
| 1452 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 1453 | connection_.NeuterUnencryptedPackets(); |
| 1454 | // Prevent packets from being coalesced. |
| 1455 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1456 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1457 | if (version().SupportsAntiAmplificationLimit()) { |
| 1458 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1459 | } |
| 1460 | // Clear direct_peer_address. |
| 1461 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1462 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1463 | // this test. |
| 1464 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1465 | QuicSocketAddress()); |
| 1466 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1467 | |
| 1468 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1469 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 1470 | } else { |
| 1471 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 1472 | } |
| 1473 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 1474 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1475 | kPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 1476 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1477 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1478 | QuicConfig config; |
| 1479 | config.SetInitialReceivedConnectionOptions(QuicTagVector{kRVCM}); |
| 1480 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 1481 | connection_.SetFromConfig(config); |
| 1482 | connection_.set_sent_server_preferred_address(kServerPreferredAddress); |
| 1483 | } |
| 1484 | |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1485 | // Receive server preferred address. |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 1486 | void ServerPreferredAddressInit(QuicConfig& config) { |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1487 | ASSERT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 1488 | ASSERT_TRUE(version().HasIetfQuicFrames()); |
| 1489 | ASSERT_TRUE(connection_.self_address().host().IsIPv6()); |
| 1490 | SetQuicReloadableFlag(quic_connection_migration_use_new_cid_v2, true); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1491 | const QuicConnectionId connection_id = TestConnectionId(17); |
| 1492 | const StatelessResetToken reset_token = |
| 1493 | QuicUtils::GenerateStatelessResetToken(connection_id); |
| 1494 | |
| 1495 | connection_.CreateConnectionIdManager(); |
| 1496 | |
| 1497 | connection_.SendCryptoStreamData(); |
| 1498 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 1499 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1500 | QuicAckFrame frame = InitAckFrame(1); |
| 1501 | // Received ACK for packet 1. |
| 1502 | ProcessFramePacketAtLevel(1, QuicFrame(&frame), ENCRYPTION_INITIAL); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 1503 | // Discard INITIAL key. |
| 1504 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 1505 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1506 | |
danzh | 72225ae | 2023-01-13 14:57:42 -0800 | [diff] [blame] | 1507 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM, kSPAD}); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1508 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, |
| 1509 | kTestStatelessResetToken); |
| 1510 | QuicConfigPeer::SetReceivedAlternateServerAddress(&config, |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 1511 | kServerPreferredAddress); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1512 | QuicConfigPeer::SetPreferredAddressConnectionIdAndToken( |
| 1513 | &config, connection_id, reset_token); |
| 1514 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 1515 | connection_.SetFromConfig(config); |
| 1516 | |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 1517 | ASSERT_TRUE( |
| 1518 | QuicConnectionPeer::GetReceivedServerPreferredAddress(&connection_) |
| 1519 | .IsInitialized()); |
| 1520 | EXPECT_EQ( |
| 1521 | kServerPreferredAddress, |
| 1522 | QuicConnectionPeer::GetReceivedServerPreferredAddress(&connection_)); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1525 | void TestClientRetryHandling(bool invalid_retry_tag, |
| 1526 | bool missing_original_id_in_config, |
| 1527 | bool wrong_original_id_in_config, |
| 1528 | bool missing_retry_id_in_config, |
| 1529 | bool wrong_retry_id_in_config); |
| 1530 | |
| 1531 | void TestReplaceConnectionIdFromInitial(); |
| 1532 | |
| 1533 | QuicConnectionId connection_id_; |
| 1534 | QuicFramer framer_; |
| 1535 | |
| 1536 | MockSendAlgorithm* send_algorithm_; |
| 1537 | std::unique_ptr<MockLossAlgorithm> loss_algorithm_; |
| 1538 | MockClock clock_; |
| 1539 | MockRandom random_generator_; |
| 1540 | quiche::SimpleBufferAllocator buffer_allocator_; |
| 1541 | std::unique_ptr<TestConnectionHelper> helper_; |
| 1542 | std::unique_ptr<TestAlarmFactory> alarm_factory_; |
| 1543 | QuicFramer peer_framer_; |
| 1544 | QuicPacketCreator peer_creator_; |
| 1545 | std::unique_ptr<TestPacketWriter> writer_; |
| 1546 | TestConnection connection_; |
| 1547 | QuicPacketCreator* creator_; |
| 1548 | QuicSentPacketManager* manager_; |
| 1549 | StrictMock<MockQuicConnectionVisitor> visitor_; |
| 1550 | |
| 1551 | QuicStreamFrame frame1_; |
| 1552 | QuicStreamFrame frame2_; |
| 1553 | QuicCryptoFrame crypto_frame_; |
| 1554 | QuicAckFrame ack_; |
| 1555 | QuicStopWaitingFrame stop_waiting_; |
| 1556 | QuicPacketNumberLength packet_number_length_; |
| 1557 | QuicConnectionIdIncluded connection_id_included_; |
| 1558 | |
| 1559 | SimpleSessionNotifier notifier_; |
| 1560 | |
| 1561 | QuicConnectionCloseFrame saved_connection_close_frame_; |
| 1562 | int connection_close_frame_count_; |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 1563 | MockConnectionIdGenerator connection_id_generator_; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1564 | }; |
| 1565 | |
| 1566 | // Run all end to end tests with all supported versions. |
| 1567 | INSTANTIATE_TEST_SUITE_P(QuicConnectionTests, QuicConnectionTest, |
| 1568 | ::testing::ValuesIn(GetTestParams()), |
| 1569 | ::testing::PrintToStringParamName()); |
| 1570 | |
| 1571 | // These two tests ensure that the QuicErrorCode mapping works correctly. |
| 1572 | // Both tests expect to see a Google QUIC close if not running IETF QUIC. |
| 1573 | // If running IETF QUIC, the first will generate a transport connection |
| 1574 | // close, the second an application connection close. |
| 1575 | // The connection close codes for the two tests are manually chosen; |
| 1576 | // they are expected to always map to transport- and application- |
| 1577 | // closes, respectively. If that changes, new codes should be chosen. |
| 1578 | TEST_P(QuicConnectionTest, CloseErrorCodeTestTransport) { |
| 1579 | EXPECT_TRUE(connection_.connected()); |
| 1580 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 1581 | connection_.CloseConnection( |
| 1582 | IETF_QUIC_PROTOCOL_VIOLATION, "Should be transport close", |
| 1583 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1584 | EXPECT_FALSE(connection_.connected()); |
| 1585 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 1586 | } |
| 1587 | |
| 1588 | // Test that the IETF QUIC Error code mapping function works |
| 1589 | // properly for application connection close codes. |
| 1590 | TEST_P(QuicConnectionTest, CloseErrorCodeTestApplication) { |
| 1591 | EXPECT_TRUE(connection_.connected()); |
| 1592 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 1593 | connection_.CloseConnection( |
| 1594 | QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE, |
| 1595 | "Should be application close", |
| 1596 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1597 | EXPECT_FALSE(connection_.connected()); |
| 1598 | TestConnectionCloseQuicErrorCode(QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE); |
| 1599 | } |
| 1600 | |
| 1601 | TEST_P(QuicConnectionTest, SelfAddressChangeAtClient) { |
| 1602 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 1603 | |
| 1604 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 1605 | EXPECT_TRUE(connection_.connected()); |
| 1606 | |
| 1607 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1608 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1609 | } else { |
| 1610 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1611 | } |
| 1612 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 1613 | ENCRYPTION_INITIAL); |
| 1614 | // Cause change in self_address. |
| 1615 | QuicIpAddress host; |
| 1616 | host.FromString("1.1.1.1"); |
| 1617 | QuicSocketAddress self_address(host, 123); |
| 1618 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1619 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1620 | } else { |
| 1621 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1622 | } |
| 1623 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address, kPeerAddress, |
| 1624 | ENCRYPTION_INITIAL); |
| 1625 | EXPECT_TRUE(connection_.connected()); |
| 1626 | } |
| 1627 | |
| 1628 | TEST_P(QuicConnectionTest, SelfAddressChangeAtServer) { |
| 1629 | set_perspective(Perspective::IS_SERVER); |
| 1630 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1631 | |
| 1632 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1633 | EXPECT_TRUE(connection_.connected()); |
| 1634 | |
| 1635 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1636 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1637 | } else { |
| 1638 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1639 | } |
| 1640 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 1641 | ENCRYPTION_INITIAL); |
| 1642 | // Cause change in self_address. |
| 1643 | QuicIpAddress host; |
| 1644 | host.FromString("1.1.1.1"); |
| 1645 | QuicSocketAddress self_address(host, 123); |
| 1646 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 1647 | EXPECT_CALL(visitor_, AllowSelfAddressChange()).WillOnce(Return(false)); |
fayang | 1902794 | 2022-05-16 10:29:29 -0700 | [diff] [blame] | 1648 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address, kPeerAddress, |
| 1649 | ENCRYPTION_INITIAL); |
| 1650 | EXPECT_TRUE(connection_.connected()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1651 | EXPECT_EQ(1u, connection_.GetStats().packets_dropped); |
| 1652 | } |
| 1653 | |
| 1654 | TEST_P(QuicConnectionTest, AllowSelfAddressChangeToMappedIpv4AddressAtServer) { |
| 1655 | set_perspective(Perspective::IS_SERVER); |
| 1656 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1657 | |
| 1658 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1659 | EXPECT_TRUE(connection_.connected()); |
| 1660 | |
| 1661 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1662 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(3); |
| 1663 | } else { |
| 1664 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(3); |
| 1665 | } |
| 1666 | QuicIpAddress host; |
| 1667 | host.FromString("1.1.1.1"); |
| 1668 | QuicSocketAddress self_address1(host, 443); |
| 1669 | connection_.SetSelfAddress(self_address1); |
| 1670 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address1, |
| 1671 | kPeerAddress, ENCRYPTION_INITIAL); |
| 1672 | // Cause self_address change to mapped Ipv4 address. |
| 1673 | QuicIpAddress host2; |
| 1674 | host2.FromString( |
| 1675 | absl::StrCat("::ffff:", connection_.self_address().host().ToString())); |
| 1676 | QuicSocketAddress self_address2(host2, connection_.self_address().port()); |
| 1677 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address2, |
| 1678 | kPeerAddress, ENCRYPTION_INITIAL); |
| 1679 | EXPECT_TRUE(connection_.connected()); |
| 1680 | // self_address change back to Ipv4 address. |
| 1681 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address1, |
| 1682 | kPeerAddress, ENCRYPTION_INITIAL); |
| 1683 | EXPECT_TRUE(connection_.connected()); |
| 1684 | } |
| 1685 | |
| 1686 | TEST_P(QuicConnectionTest, ClientAddressChangeAndPacketReordered) { |
| 1687 | set_perspective(Perspective::IS_SERVER); |
| 1688 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1689 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1690 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1691 | |
| 1692 | // Clear direct_peer_address. |
| 1693 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1694 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1695 | // this test. |
| 1696 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1697 | QuicSocketAddress()); |
| 1698 | |
| 1699 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1700 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 1701 | } else { |
| 1702 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 1703 | } |
| 1704 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); |
| 1705 | const QuicSocketAddress kNewPeerAddress = |
| 1706 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 1707 | /*port=*/23456); |
| 1708 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1709 | kNewPeerAddress, ENCRYPTION_INITIAL); |
| 1710 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1711 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1712 | |
| 1713 | // Decrease packet number to simulate out-of-order packets. |
| 1714 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 4); |
| 1715 | // This is an old packet, do not migrate. |
| 1716 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 1717 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 1718 | ENCRYPTION_INITIAL); |
| 1719 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1720 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1721 | } |
| 1722 | |
| 1723 | TEST_P(QuicConnectionTest, PeerPortChangeAtServer) { |
| 1724 | set_perspective(Perspective::IS_SERVER); |
| 1725 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1726 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1727 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1728 | // Prevent packets from being coalesced. |
| 1729 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1730 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1731 | if (version().SupportsAntiAmplificationLimit()) { |
| 1732 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1733 | } |
| 1734 | |
| 1735 | // Clear direct_peer_address. |
| 1736 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1737 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1738 | // this test. |
| 1739 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1740 | QuicSocketAddress()); |
| 1741 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1742 | |
| 1743 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 1744 | QuicTime::Delta default_init_rtt = rtt_stats->initial_rtt(); |
| 1745 | rtt_stats->set_initial_rtt(default_init_rtt * 2); |
| 1746 | EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt()); |
| 1747 | |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 1748 | QuicSentPacketManagerPeer::SetConsecutivePtoCount(manager_, 1); |
| 1749 | EXPECT_EQ(1u, manager_->GetConsecutivePtoCount()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1750 | |
| 1751 | const QuicSocketAddress kNewPeerAddress = |
| 1752 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 1753 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 1754 | .WillOnce(Invoke( |
| 1755 | [=]() { EXPECT_EQ(kPeerAddress, connection_.peer_address()); })) |
| 1756 | .WillOnce(Invoke( |
| 1757 | [=]() { EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); })); |
| 1758 | QuicFrames frames; |
| 1759 | frames.push_back(QuicFrame(frame1_)); |
| 1760 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 1761 | ENCRYPTION_FORWARD_SECURE); |
| 1762 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1763 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1764 | |
| 1765 | // Process another packet with a different peer address on server side will |
| 1766 | // start connection migration. |
| 1767 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 1768 | QuicFrames frames2; |
| 1769 | frames2.push_back(QuicFrame(frame2_)); |
| 1770 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 1771 | ENCRYPTION_FORWARD_SECURE); |
| 1772 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1773 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1774 | // PORT_CHANGE shouldn't state change in sent packet manager. |
| 1775 | EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt()); |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 1776 | EXPECT_EQ(1u, manager_->GetConsecutivePtoCount()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1777 | EXPECT_EQ(manager_->GetSendAlgorithm(), send_algorithm_); |
| 1778 | if (connection_.validate_client_address()) { |
| 1779 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 1780 | EXPECT_EQ(1u, connection_.GetStats().num_validated_peer_migration); |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 1781 | EXPECT_EQ(1u, connection_.num_linkable_client_migration()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | TEST_P(QuicConnectionTest, PeerIpAddressChangeAtServer) { |
| 1786 | set_perspective(Perspective::IS_SERVER); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 1787 | if (!connection_.validate_client_address() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 1788 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1789 | return; |
| 1790 | } |
| 1791 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1792 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1793 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1794 | // Discard INITIAL key. |
| 1795 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 1796 | connection_.NeuterUnencryptedPackets(); |
| 1797 | // Prevent packets from being coalesced. |
| 1798 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1799 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1800 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1801 | connection_.OnHandshakeComplete(); |
| 1802 | |
| 1803 | // Enable 5 RTO |
| 1804 | QuicConfig config; |
| 1805 | QuicTagVector connection_options; |
| 1806 | connection_options.push_back(k5RTO); |
| 1807 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 1808 | QuicConfigPeer::SetNegotiated(&config, true); |
| 1809 | QuicConfigPeer::SetReceivedOriginalConnectionId(&config, |
| 1810 | connection_.connection_id()); |
| 1811 | QuicConfigPeer::SetReceivedInitialSourceConnectionId(&config, |
| 1812 | QuicConnectionId()); |
| 1813 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 1814 | connection_.SetFromConfig(config); |
| 1815 | |
| 1816 | // Clear direct_peer_address. |
| 1817 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1818 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1819 | // this test. |
| 1820 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1821 | QuicSocketAddress()); |
| 1822 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1823 | |
| 1824 | const QuicSocketAddress kNewPeerAddress = |
| 1825 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 1826 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 1827 | .WillOnce(Invoke( |
| 1828 | [=]() { EXPECT_EQ(kPeerAddress, connection_.peer_address()); })) |
| 1829 | .WillOnce(Invoke( |
| 1830 | [=]() { EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); })); |
| 1831 | QuicFrames frames; |
| 1832 | frames.push_back(QuicFrame(frame1_)); |
| 1833 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 1834 | ENCRYPTION_FORWARD_SECURE); |
| 1835 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1836 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1837 | |
| 1838 | // Send some data to make connection has packets in flight. |
| 1839 | connection_.SendStreamData3(); |
| 1840 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 1841 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 1842 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 1843 | |
| 1844 | // Process another packet with a different peer address on server side will |
| 1845 | // start connection migration. |
| 1846 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 1847 | // IETF QUIC send algorithm should be changed to a different object, so no |
| 1848 | // OnPacketSent() called on the old send algorithm. |
| 1849 | EXPECT_CALL(*send_algorithm_, |
| 1850 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 1851 | .Times(0); |
| 1852 | // Do not propagate OnCanWrite() to session notifier. |
| 1853 | EXPECT_CALL(visitor_, OnCanWrite()).Times(AtLeast(1u)); |
| 1854 | |
| 1855 | QuicFrames frames2; |
| 1856 | frames2.push_back(QuicFrame(frame2_)); |
| 1857 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 1858 | ENCRYPTION_FORWARD_SECURE); |
| 1859 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1860 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1861 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 1862 | connection_.active_effective_peer_migration_type()); |
| 1863 | EXPECT_FALSE(connection_.BlackholeDetectionInProgress()); |
| 1864 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 1865 | |
| 1866 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 1867 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 1868 | QuicPathFrameBuffer payload = |
| 1869 | writer_->path_challenge_frames().front().data_buffer; |
| 1870 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 1871 | send_algorithm_); |
| 1872 | // Switch to use the mock send algorithm. |
| 1873 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 1874 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 1875 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 1876 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 1877 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 1878 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 1879 | .Times(AnyNumber()) |
| 1880 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 1881 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 1882 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 1883 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 1884 | connection_.SetSendAlgorithm(send_algorithm_); |
| 1885 | |
| 1886 | // PATH_CHALLENGE is expanded upto the max packet size which may exceeds the |
| 1887 | // anti-amplification limit. |
| 1888 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 1889 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1890 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1891 | EXPECT_EQ(1u, |
| 1892 | connection_.GetStats().num_reverse_path_validtion_upon_migration); |
| 1893 | |
| 1894 | // Verify server is throttled by anti-amplification limit. |
| 1895 | connection_.SendCryptoDataWithString("foo", 0); |
| 1896 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 1897 | |
| 1898 | // Receiving an ACK to the packet sent after changing peer address doesn't |
| 1899 | // finish migration validation. |
| 1900 | QuicAckFrame ack_frame = InitAckFrame(2); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 1901 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1902 | ProcessFramePacketWithAddresses(QuicFrame(&ack_frame), kSelfAddress, |
| 1903 | kNewPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 1904 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1905 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1906 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 1907 | connection_.active_effective_peer_migration_type()); |
| 1908 | |
| 1909 | // Receiving PATH_RESPONSE should lift the anti-amplification limit. |
| 1910 | QuicFrames frames3; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 1911 | frames3.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1912 | EXPECT_CALL(visitor_, MaybeSendAddressToken()); |
| 1913 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1914 | .Times(testing::AtLeast(1u)); |
| 1915 | ProcessFramesPacketWithAddresses(frames3, kSelfAddress, kNewPeerAddress, |
| 1916 | ENCRYPTION_FORWARD_SECURE); |
| 1917 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 1918 | |
| 1919 | // Verify the anti-amplification limit is lifted by sending a packet larger |
| 1920 | // than the anti-amplification limit. |
| 1921 | connection_.SendCryptoDataWithString(std::string(1200, 'a'), 0); |
| 1922 | EXPECT_EQ(1u, connection_.GetStats().num_validated_peer_migration); |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 1923 | EXPECT_EQ(1u, connection_.num_linkable_client_migration()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | TEST_P(QuicConnectionTest, PeerIpAddressChangeAtServerWithMissingConnectionId) { |
| 1927 | set_perspective(Perspective::IS_SERVER); |
| 1928 | if (!connection_.connection_migration_use_new_cid()) { |
| 1929 | return; |
| 1930 | } |
| 1931 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1932 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1933 | |
| 1934 | QuicConnectionId client_cid0 = TestConnectionId(1); |
| 1935 | QuicConnectionId client_cid1 = TestConnectionId(3); |
| 1936 | QuicConnectionId server_cid1; |
| 1937 | SetClientConnectionId(client_cid0); |
| 1938 | connection_.CreateConnectionIdManager(); |
| 1939 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1940 | // Prevent packets from being coalesced. |
| 1941 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1942 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 1943 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1944 | |
| 1945 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 1946 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 1947 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 1948 | .WillOnce(Return(TestConnectionId(456))); |
| 1949 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 1950 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 1951 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 1952 | server_cid1 = cid; |
| 1953 | return true; |
| 1954 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1955 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 1956 | connection_.OnHandshakeComplete(); |
| 1957 | |
| 1958 | // Clear direct_peer_address. |
| 1959 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1960 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1961 | // this test. |
| 1962 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1963 | QuicSocketAddress()); |
| 1964 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1965 | |
| 1966 | const QuicSocketAddress kNewPeerAddress = |
| 1967 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 1968 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(2); |
| 1969 | QuicFrames frames; |
| 1970 | frames.push_back(QuicFrame(frame1_)); |
| 1971 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 1972 | ENCRYPTION_FORWARD_SECURE); |
| 1973 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1974 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1975 | |
| 1976 | // Send some data to make connection has packets in flight. |
| 1977 | connection_.SendStreamData3(); |
| 1978 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 1979 | |
| 1980 | // Process another packet with a different peer address on server side will |
| 1981 | // start connection migration. |
| 1982 | peer_creator_.SetServerConnectionId(server_cid1); |
| 1983 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 1984 | // Do not propagate OnCanWrite() to session notifier. |
| 1985 | EXPECT_CALL(visitor_, OnCanWrite()).Times(AtLeast(1u)); |
| 1986 | |
| 1987 | QuicFrames frames2; |
| 1988 | frames2.push_back(QuicFrame(frame2_)); |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 1989 | if (GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 1990 | frames2.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 1991 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1992 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 1993 | ENCRYPTION_FORWARD_SECURE); |
| 1994 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1995 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1996 | |
| 1997 | // Writing path response & reverse path challenge is blocked due to missing |
| 1998 | // client connection ID, i.e., packets_write_attempts is unchanged. |
| 1999 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 2000 | |
| 2001 | // Receives new client CID from client would unblock write. |
| 2002 | QuicNewConnectionIdFrame new_cid_frame; |
| 2003 | new_cid_frame.connection_id = client_cid1; |
| 2004 | new_cid_frame.sequence_number = 1u; |
| 2005 | new_cid_frame.retire_prior_to = 0u; |
| 2006 | connection_.OnNewConnectionIdFrame(new_cid_frame); |
| 2007 | connection_.SendStreamData3(); |
| 2008 | |
| 2009 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 2010 | } |
| 2011 | |
| 2012 | TEST_P(QuicConnectionTest, EffectivePeerAddressChangeAtServer) { |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 2013 | if (GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 2014 | return; |
| 2015 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2016 | set_perspective(Perspective::IS_SERVER); |
| 2017 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2018 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2019 | if (version().SupportsAntiAmplificationLimit()) { |
| 2020 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2021 | } |
| 2022 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 2023 | // Discard INITIAL key. |
| 2024 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 2025 | connection_.NeuterUnencryptedPackets(); |
| 2026 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 2027 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 2028 | |
| 2029 | // Clear direct_peer_address. |
| 2030 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2031 | // Clear effective_peer_address, it is different from direct_peer_address for |
| 2032 | // this test. |
| 2033 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2034 | QuicSocketAddress()); |
| 2035 | const QuicSocketAddress kEffectivePeerAddress = |
| 2036 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/43210); |
| 2037 | connection_.ReturnEffectivePeerAddressForNextPacket(kEffectivePeerAddress); |
| 2038 | |
| 2039 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2040 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2041 | } else { |
| 2042 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2043 | } |
| 2044 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 2045 | ENCRYPTION_FORWARD_SECURE); |
| 2046 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2047 | EXPECT_EQ(kEffectivePeerAddress, connection_.effective_peer_address()); |
| 2048 | |
| 2049 | // Process another packet with the same direct peer address and different |
| 2050 | // effective peer address on server side will start connection migration. |
| 2051 | const QuicSocketAddress kNewEffectivePeerAddress = |
| 2052 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/54321); |
| 2053 | connection_.ReturnEffectivePeerAddressForNextPacket(kNewEffectivePeerAddress); |
| 2054 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2055 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 2056 | ENCRYPTION_FORWARD_SECURE); |
| 2057 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2058 | EXPECT_EQ(kNewEffectivePeerAddress, connection_.effective_peer_address()); |
| 2059 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 2060 | if (connection_.validate_client_address()) { |
| 2061 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 2062 | EXPECT_EQ(1u, connection_.GetStats().num_validated_peer_migration); |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 2063 | EXPECT_EQ(1u, connection_.num_linkable_client_migration()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | // Process another packet with a different direct peer address and the same |
| 2067 | // effective peer address on server side will not start connection migration. |
| 2068 | const QuicSocketAddress kNewPeerAddress = |
| 2069 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2070 | connection_.ReturnEffectivePeerAddressForNextPacket(kNewEffectivePeerAddress); |
| 2071 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2072 | |
| 2073 | if (!connection_.validate_client_address()) { |
| 2074 | // ack_frame is used to complete the migration started by the last packet, |
| 2075 | // we need to make sure a new migration does not start after the previous |
| 2076 | // one is completed. |
| 2077 | QuicAckFrame ack_frame = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 2078 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2079 | ProcessFramePacketWithAddresses(QuicFrame(&ack_frame), kSelfAddress, |
| 2080 | kNewPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 2081 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2082 | EXPECT_EQ(kNewEffectivePeerAddress, connection_.effective_peer_address()); |
| 2083 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 2084 | } |
| 2085 | |
| 2086 | // Process another packet with different direct peer address and different |
| 2087 | // effective peer address on server side will start connection migration. |
| 2088 | const QuicSocketAddress kNewerEffectivePeerAddress = |
| 2089 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/65432); |
| 2090 | const QuicSocketAddress kFinalPeerAddress = |
| 2091 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/34567); |
| 2092 | connection_.ReturnEffectivePeerAddressForNextPacket( |
| 2093 | kNewerEffectivePeerAddress); |
| 2094 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2095 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2096 | kFinalPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 2097 | EXPECT_EQ(kFinalPeerAddress, connection_.peer_address()); |
| 2098 | EXPECT_EQ(kNewerEffectivePeerAddress, connection_.effective_peer_address()); |
| 2099 | if (connection_.validate_client_address()) { |
| 2100 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 2101 | EXPECT_EQ(send_algorithm_, |
| 2102 | connection_.sent_packet_manager().GetSendAlgorithm()); |
| 2103 | EXPECT_EQ(2u, connection_.GetStats().num_validated_peer_migration); |
| 2104 | } |
| 2105 | |
| 2106 | // While the previous migration is ongoing, process another packet with the |
| 2107 | // same direct peer address and different effective peer address on server |
| 2108 | // side will start a new connection migration. |
| 2109 | const QuicSocketAddress kNewestEffectivePeerAddress = |
| 2110 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/65430); |
| 2111 | connection_.ReturnEffectivePeerAddressForNextPacket( |
| 2112 | kNewestEffectivePeerAddress); |
| 2113 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 2114 | if (!connection_.validate_client_address()) { |
| 2115 | EXPECT_CALL(*send_algorithm_, OnConnectionMigration()).Times(1); |
| 2116 | } |
| 2117 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2118 | kFinalPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 2119 | EXPECT_EQ(kFinalPeerAddress, connection_.peer_address()); |
| 2120 | EXPECT_EQ(kNewestEffectivePeerAddress, connection_.effective_peer_address()); |
| 2121 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 2122 | connection_.active_effective_peer_migration_type()); |
| 2123 | if (connection_.validate_client_address()) { |
| 2124 | EXPECT_NE(send_algorithm_, |
| 2125 | connection_.sent_packet_manager().GetSendAlgorithm()); |
| 2126 | EXPECT_EQ(kFinalPeerAddress, writer_->last_write_peer_address()); |
| 2127 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 2128 | EXPECT_EQ(0u, connection_.GetStats() |
| 2129 | .num_peer_migration_while_validating_default_path); |
| 2130 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | // Regression test for b/200020764. |
| 2135 | TEST_P(QuicConnectionTest, ConnectionMigrationWithPendingPaddingBytes) { |
| 2136 | // TODO(haoyuewang) Move these test setup code to a common member function. |
| 2137 | set_perspective(Perspective::IS_SERVER); |
| 2138 | if (!connection_.connection_migration_use_new_cid()) { |
| 2139 | return; |
| 2140 | } |
| 2141 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2142 | connection_.CreateConnectionIdManager(); |
| 2143 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 2144 | QuicConnectionPeer::SetPeerAddress(&connection_, kPeerAddress); |
| 2145 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, kPeerAddress); |
| 2146 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2147 | |
| 2148 | // Sends new server CID to client. |
| 2149 | QuicConnectionId new_cid; |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 2150 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 2151 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 2152 | .WillOnce(Return(TestConnectionId(456))); |
| 2153 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 2154 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 2155 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 2156 | new_cid = cid; |
| 2157 | return true; |
| 2158 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2159 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 2160 | // Discard INITIAL key. |
| 2161 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 2162 | connection_.NeuterUnencryptedPackets(); |
| 2163 | connection_.OnHandshakeComplete(); |
| 2164 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 2165 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 2166 | |
| 2167 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 2168 | packet_creator->FlushCurrentPacket(); |
| 2169 | packet_creator->AddPendingPadding(50u); |
| 2170 | const QuicSocketAddress kPeerAddress3 = |
| 2171 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/56789); |
| 2172 | auto ack_frame = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 2173 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2174 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2175 | ProcessFramesPacketWithAddresses({QuicFrame(&ack_frame)}, kSelfAddress, |
| 2176 | kPeerAddress3, ENCRYPTION_FORWARD_SECURE); |
| 2177 | if (GetQuicReloadableFlag( |
| 2178 | quic_flush_pending_frames_and_padding_bytes_on_migration)) { |
| 2179 | // Any pending frames/padding should be flushed before default_path_ is |
| 2180 | // temporarily reset. |
| 2181 | ASSERT_EQ(connection_.self_address_on_default_path_while_sending_packet() |
| 2182 | .host() |
| 2183 | .address_family(), |
| 2184 | IpAddressFamily::IP_V6); |
| 2185 | } else { |
| 2186 | ASSERT_EQ(connection_.self_address_on_default_path_while_sending_packet() |
| 2187 | .host() |
| 2188 | .address_family(), |
| 2189 | IpAddressFamily::IP_UNSPEC); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | // Regression test for b/196208556. |
| 2194 | TEST_P(QuicConnectionTest, |
| 2195 | ReversePathValidationResponseReceivedFromUnexpectedPeerAddress) { |
| 2196 | set_perspective(Perspective::IS_SERVER); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 2197 | if (!connection_.connection_migration_use_new_cid() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 2198 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2199 | return; |
| 2200 | } |
| 2201 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2202 | connection_.CreateConnectionIdManager(); |
| 2203 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 2204 | QuicConnectionPeer::SetPeerAddress(&connection_, kPeerAddress); |
| 2205 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, kPeerAddress); |
| 2206 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2207 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2208 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2209 | |
| 2210 | // Sends new server CID to client. |
| 2211 | QuicConnectionId new_cid; |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 2212 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 2213 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 2214 | .WillOnce(Return(TestConnectionId(456))); |
| 2215 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 2216 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 2217 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 2218 | new_cid = cid; |
| 2219 | return true; |
| 2220 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2221 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 2222 | // Discard INITIAL key. |
| 2223 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 2224 | connection_.NeuterUnencryptedPackets(); |
| 2225 | connection_.OnHandshakeComplete(); |
| 2226 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 2227 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 2228 | |
| 2229 | // Process a non-probing packet to migrate to path 2 and kick off reverse path |
| 2230 | // validation. |
| 2231 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 2232 | const QuicSocketAddress kPeerAddress2 = |
| 2233 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 2234 | peer_creator_.SetServerConnectionId(new_cid); |
| 2235 | ProcessFramesPacketWithAddresses({QuicFrame(QuicPingFrame())}, kSelfAddress, |
| 2236 | kPeerAddress2, ENCRYPTION_FORWARD_SECURE); |
| 2237 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 2238 | QuicPathFrameBuffer reverse_path_challenge_payload = |
| 2239 | writer_->path_challenge_frames().front().data_buffer; |
| 2240 | |
| 2241 | // Receiveds a packet from path 3 with PATH_RESPONSE frame intended to |
| 2242 | // validate path 2 and a non-probing frame. |
| 2243 | { |
| 2244 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 2245 | const QuicSocketAddress kPeerAddress3 = |
| 2246 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/56789); |
| 2247 | auto ack_frame = InitAckFrame(1); |
| 2248 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV4_TO_IPV6_CHANGE)).Times(1); |
| 2249 | EXPECT_CALL(visitor_, MaybeSendAddressToken()).WillOnce(Invoke([this]() { |
| 2250 | connection_.SendControlFrame( |
| 2251 | QuicFrame(new QuicNewTokenFrame(1, "new_token"))); |
| 2252 | return true; |
| 2253 | })); |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 2254 | ProcessFramesPacketWithAddresses( |
| 2255 | {QuicFrame(QuicPathResponseFrame(0, reverse_path_challenge_payload)), |
| 2256 | QuicFrame(&ack_frame)}, |
| 2257 | kSelfAddress, kPeerAddress3, ENCRYPTION_FORWARD_SECURE); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | TEST_P(QuicConnectionTest, ReversePathValidationFailureAtServer) { |
| 2262 | set_perspective(Perspective::IS_SERVER); |
| 2263 | if (!connection_.connection_migration_use_new_cid()) { |
| 2264 | return; |
| 2265 | } |
| 2266 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2267 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2268 | SetClientConnectionId(TestConnectionId(1)); |
| 2269 | connection_.CreateConnectionIdManager(); |
| 2270 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 2271 | // Discard INITIAL key. |
| 2272 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 2273 | connection_.NeuterUnencryptedPackets(); |
| 2274 | // Prevent packets from being coalesced. |
| 2275 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 2276 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 2277 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2278 | |
| 2279 | QuicConnectionId client_cid0 = connection_.client_connection_id(); |
| 2280 | QuicConnectionId client_cid1 = TestConnectionId(2); |
| 2281 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 2282 | QuicConnectionId server_cid1; |
| 2283 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 2284 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 2285 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 2286 | .WillOnce(Return(TestConnectionId(456))); |
| 2287 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 2288 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 2289 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 2290 | server_cid1 = cid; |
| 2291 | return true; |
| 2292 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2293 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 2294 | connection_.OnHandshakeComplete(); |
| 2295 | // Receives new client CID from client. |
| 2296 | QuicNewConnectionIdFrame new_cid_frame; |
| 2297 | new_cid_frame.connection_id = client_cid1; |
| 2298 | new_cid_frame.sequence_number = 1u; |
| 2299 | new_cid_frame.retire_prior_to = 0u; |
| 2300 | connection_.OnNewConnectionIdFrame(new_cid_frame); |
| 2301 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 2302 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 2303 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 2304 | |
| 2305 | // Clear direct_peer_address. |
| 2306 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2307 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2308 | // this test. |
| 2309 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2310 | QuicSocketAddress()); |
| 2311 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2312 | |
| 2313 | const QuicSocketAddress kNewPeerAddress = |
| 2314 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 2315 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 2316 | .WillOnce(Invoke( |
| 2317 | [=]() { EXPECT_EQ(kPeerAddress, connection_.peer_address()); })) |
| 2318 | .WillOnce(Invoke( |
| 2319 | [=]() { EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); })); |
| 2320 | QuicFrames frames; |
| 2321 | frames.push_back(QuicFrame(frame1_)); |
| 2322 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 2323 | ENCRYPTION_FORWARD_SECURE); |
| 2324 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2325 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2326 | |
| 2327 | // Process another packet with a different peer address on server side will |
| 2328 | // start connection migration. |
| 2329 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 2330 | // IETF QUIC send algorithm should be changed to a different object, so no |
| 2331 | // OnPacketSent() called on the old send algorithm. |
| 2332 | EXPECT_CALL(*send_algorithm_, OnConnectionMigration()).Times(0); |
| 2333 | |
| 2334 | QuicFrames frames2; |
| 2335 | frames2.push_back(QuicFrame(frame2_)); |
| 2336 | QuicPaddingFrame padding; |
| 2337 | frames2.push_back(QuicFrame(padding)); |
| 2338 | peer_creator_.SetServerConnectionId(server_cid1); |
| 2339 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 2340 | ENCRYPTION_FORWARD_SECURE); |
| 2341 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2342 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2343 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 2344 | connection_.active_effective_peer_migration_type()); |
| 2345 | EXPECT_LT(0u, writer_->packets_write_attempts()); |
| 2346 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 2347 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 2348 | send_algorithm_); |
| 2349 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 2350 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2351 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2352 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 2353 | const auto* alternative_path = |
| 2354 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 2355 | EXPECT_EQ(default_path->client_connection_id, client_cid1); |
| 2356 | EXPECT_EQ(default_path->server_connection_id, server_cid1); |
| 2357 | EXPECT_EQ(alternative_path->client_connection_id, client_cid0); |
| 2358 | EXPECT_EQ(alternative_path->server_connection_id, server_cid0); |
| 2359 | EXPECT_EQ(packet_creator->GetDestinationConnectionId(), client_cid1); |
| 2360 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid1); |
| 2361 | |
| 2362 | for (size_t i = 0; i < QuicPathValidator::kMaxRetryTimes; ++i) { |
| 2363 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 2364 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 2365 | QuicPathValidatorPeer::retry_timer( |
| 2366 | QuicConnectionPeer::path_validator(&connection_))) |
| 2367 | ->Fire(); |
| 2368 | } |
| 2369 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 2370 | connection_.active_effective_peer_migration_type()); |
| 2371 | |
| 2372 | // Make sure anti-amplification limit is not reached. |
| 2373 | ProcessFramesPacketWithAddresses( |
| 2374 | {QuicFrame(QuicPingFrame()), QuicFrame(QuicPaddingFrame())}, kSelfAddress, |
| 2375 | kNewPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 2376 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 2377 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 2378 | |
| 2379 | // Advance the time so that the reverse path validation times out. |
| 2380 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 2381 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 2382 | QuicPathValidatorPeer::retry_timer( |
| 2383 | QuicConnectionPeer::path_validator(&connection_))) |
| 2384 | ->Fire(); |
| 2385 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 2386 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2387 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2388 | EXPECT_EQ(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 2389 | send_algorithm_); |
| 2390 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 2391 | |
| 2392 | // Verify that default_path_ is reverted and alternative_path_ is cleared. |
| 2393 | EXPECT_EQ(default_path->client_connection_id, client_cid0); |
| 2394 | EXPECT_EQ(default_path->server_connection_id, server_cid0); |
| 2395 | EXPECT_TRUE(alternative_path->server_connection_id.IsEmpty()); |
| 2396 | EXPECT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 2397 | auto* retire_peer_issued_cid_alarm = |
| 2398 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 2399 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 2400 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/1u)); |
| 2401 | retire_peer_issued_cid_alarm->Fire(); |
| 2402 | EXPECT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 2403 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 2404 | } |
| 2405 | |
| 2406 | TEST_P(QuicConnectionTest, ReceivePathProbeWithNoAddressChangeAtServer) { |
| 2407 | PathProbeTestInit(Perspective::IS_SERVER); |
| 2408 | |
| 2409 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2410 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, false)).Times(0); |
| 2411 | |
| 2412 | // Process a padded PING packet with no peer address change on server side |
| 2413 | // will be ignored. But a PATH CHALLENGE packet with no peer address change |
| 2414 | // will be considered as path probing. |
| 2415 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2416 | |
| 2417 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2418 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2419 | probing_packet->encrypted_length), |
| 2420 | clock_.Now())); |
| 2421 | |
| 2422 | uint64_t num_probing_received = |
| 2423 | connection_.GetStats().num_connectivity_probing_received; |
| 2424 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, *received); |
| 2425 | |
| 2426 | EXPECT_EQ( |
| 2427 | num_probing_received + (GetParam().version.HasIetfQuicFrames() ? 1u : 0u), |
| 2428 | connection_.GetStats().num_connectivity_probing_received); |
| 2429 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2430 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2431 | } |
| 2432 | |
| 2433 | // Regression test for b/150161358. |
| 2434 | TEST_P(QuicConnectionTest, BufferedMtuPacketTooBig) { |
| 2435 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 2436 | writer_->SetWriteBlocked(); |
| 2437 | |
| 2438 | // Send a MTU packet while blocked. It should be buffered. |
| 2439 | connection_.SendMtuDiscoveryPacket(kMaxOutgoingPacketSize); |
| 2440 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 2441 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 2442 | |
| 2443 | writer_->AlwaysGetPacketTooLarge(); |
| 2444 | writer_->SetWritable(); |
| 2445 | connection_.OnCanWrite(); |
| 2446 | } |
| 2447 | |
| 2448 | TEST_P(QuicConnectionTest, WriteOutOfOrderQueuedPackets) { |
| 2449 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 2450 | if (!IsDefaultTestConfiguration()) { |
| 2451 | return; |
| 2452 | } |
| 2453 | |
| 2454 | set_perspective(Perspective::IS_CLIENT); |
| 2455 | |
| 2456 | BlockOnNextWrite(); |
| 2457 | |
| 2458 | QuicStreamId stream_id = 2; |
| 2459 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 2460 | |
| 2461 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 2462 | |
| 2463 | writer_->SetWritable(); |
| 2464 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 2465 | connection_.peer_address()); |
| 2466 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 2467 | connection_.OnCanWrite(); |
| 2468 | } |
| 2469 | |
| 2470 | TEST_P(QuicConnectionTest, DiscardQueuedPacketsAfterConnectionClose) { |
| 2471 | // Regression test for b/74073386. |
| 2472 | { |
| 2473 | InSequence seq; |
| 2474 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2475 | .Times(AtLeast(1)); |
| 2476 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(AtLeast(1)); |
| 2477 | } |
| 2478 | |
| 2479 | set_perspective(Perspective::IS_CLIENT); |
| 2480 | |
| 2481 | writer_->SimulateNextPacketTooLarge(); |
| 2482 | |
| 2483 | // This packet write should fail, which should cause the connection to close |
| 2484 | // after sending a connection close packet, then the failed packet should be |
| 2485 | // queued. |
| 2486 | connection_.SendStreamDataWithString(/*id=*/2, "foo", 0, NO_FIN); |
| 2487 | |
| 2488 | EXPECT_FALSE(connection_.connected()); |
| 2489 | // No need to buffer packets. |
| 2490 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 2491 | |
| 2492 | EXPECT_EQ(0u, connection_.GetStats().packets_discarded); |
| 2493 | connection_.OnCanWrite(); |
| 2494 | EXPECT_EQ(0u, connection_.GetStats().packets_discarded); |
| 2495 | } |
| 2496 | |
| 2497 | class TestQuicPathValidationContext : public QuicPathValidationContext { |
| 2498 | public: |
| 2499 | TestQuicPathValidationContext(const QuicSocketAddress& self_address, |
| 2500 | const QuicSocketAddress& peer_address, |
| 2501 | |
| 2502 | QuicPacketWriter* writer) |
| 2503 | : QuicPathValidationContext(self_address, peer_address), |
| 2504 | writer_(writer) {} |
| 2505 | |
| 2506 | QuicPacketWriter* WriterToUse() override { return writer_; } |
| 2507 | |
| 2508 | private: |
| 2509 | QuicPacketWriter* writer_; |
| 2510 | }; |
| 2511 | |
| 2512 | class TestValidationResultDelegate : public QuicPathValidator::ResultDelegate { |
| 2513 | public: |
| 2514 | TestValidationResultDelegate(QuicConnection* connection, |
| 2515 | const QuicSocketAddress& expected_self_address, |
| 2516 | const QuicSocketAddress& expected_peer_address, |
| 2517 | bool* success) |
| 2518 | : QuicPathValidator::ResultDelegate(), |
| 2519 | connection_(connection), |
| 2520 | expected_self_address_(expected_self_address), |
| 2521 | expected_peer_address_(expected_peer_address), |
| 2522 | success_(success) {} |
| 2523 | void OnPathValidationSuccess( |
wub | 130bde9 | 2022-08-01 14:33:14 -0700 | [diff] [blame] | 2524 | std::unique_ptr<QuicPathValidationContext> context, |
| 2525 | QuicTime /*start_time*/) override { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2526 | EXPECT_EQ(expected_self_address_, context->self_address()); |
| 2527 | EXPECT_EQ(expected_peer_address_, context->peer_address()); |
| 2528 | *success_ = true; |
| 2529 | } |
| 2530 | |
| 2531 | void OnPathValidationFailure( |
| 2532 | std::unique_ptr<QuicPathValidationContext> context) override { |
| 2533 | EXPECT_EQ(expected_self_address_, context->self_address()); |
| 2534 | EXPECT_EQ(expected_peer_address_, context->peer_address()); |
| 2535 | if (connection_->perspective() == Perspective::IS_CLIENT) { |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 2536 | connection_->OnPathValidationFailureAtClient(/*is_multi_port=*/false, |
| 2537 | *context); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2538 | } |
| 2539 | *success_ = false; |
| 2540 | } |
| 2541 | |
| 2542 | private: |
| 2543 | QuicConnection* connection_; |
| 2544 | QuicSocketAddress expected_self_address_; |
| 2545 | QuicSocketAddress expected_peer_address_; |
| 2546 | bool* success_; |
| 2547 | }; |
| 2548 | |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 2549 | // A test implementation which migrates to server preferred address |
| 2550 | // on path validation suceeds. Otherwise, client cleans up alternative path. |
fayang | 5c6e701 | 2023-01-08 20:06:01 -0800 | [diff] [blame] | 2551 | class ServerPreferredAddressTestResultDelegate |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 2552 | : public QuicPathValidator::ResultDelegate { |
| 2553 | public: |
| 2554 | explicit ServerPreferredAddressTestResultDelegate(QuicConnection* connection) |
| 2555 | : connection_(connection) {} |
| 2556 | void OnPathValidationSuccess( |
| 2557 | std::unique_ptr<QuicPathValidationContext> context, |
| 2558 | QuicTime /*start_time*/) override { |
| 2559 | connection_->OnServerPreferredAddressValidated(*context, false); |
| 2560 | } |
| 2561 | |
| 2562 | void OnPathValidationFailure( |
| 2563 | std::unique_ptr<QuicPathValidationContext> context) override { |
| 2564 | connection_->OnPathValidationFailureAtClient(/*is_multi_port=*/false, |
| 2565 | *context); |
| 2566 | } |
| 2567 | |
| 2568 | protected: |
| 2569 | QuicConnection* connection() { return connection_; } |
| 2570 | |
| 2571 | private: |
| 2572 | QuicConnection* connection_; |
| 2573 | }; |
| 2574 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2575 | // Receive a path probe request at the server side, i.e., |
| 2576 | // in non-IETF version: receive a padded PING packet with a peer addess change; |
| 2577 | // in IETF version: receive a packet contains PATH CHALLENGE with peer address |
| 2578 | // change. |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 2579 | TEST_P(QuicConnectionTest, ReceivePathProbingFromNewPeerAddressAtServer) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2580 | PathProbeTestInit(Perspective::IS_SERVER); |
| 2581 | |
| 2582 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2583 | QuicPathFrameBuffer payload; |
| 2584 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2585 | EXPECT_CALL(visitor_, |
| 2586 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2587 | .Times(1); |
| 2588 | } else { |
| 2589 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2590 | if (connection_.validate_client_address()) { |
| 2591 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2592 | .Times(AtLeast(1u)) |
| 2593 | .WillOnce(Invoke([&]() { |
| 2594 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 2595 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 2596 | payload = writer_->path_challenge_frames().front().data_buffer; |
| 2597 | })); |
| 2598 | } |
| 2599 | } |
| 2600 | // Process a probing packet from a new peer address on server side |
| 2601 | // is effectively receiving a connectivity probing. |
| 2602 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 2603 | /*port=*/23456); |
| 2604 | |
| 2605 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2606 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2607 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2608 | probing_packet->encrypted_length), |
| 2609 | clock_.Now())); |
| 2610 | uint64_t num_probing_received = |
| 2611 | connection_.GetStats().num_connectivity_probing_received; |
| 2612 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2613 | |
| 2614 | EXPECT_EQ(num_probing_received + 1, |
| 2615 | connection_.GetStats().num_connectivity_probing_received); |
| 2616 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2617 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
danzh | af2e52a | 2022-04-20 07:37:03 -0700 | [diff] [blame] | 2618 | if (GetParam().version.HasIetfQuicFrames()) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2619 | QuicByteCount bytes_sent = |
| 2620 | QuicConnectionPeer::BytesSentOnAlternativePath(&connection_); |
| 2621 | EXPECT_LT(0u, bytes_sent); |
| 2622 | EXPECT_EQ(received->length(), |
| 2623 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 2624 | |
| 2625 | // Receiving one more probing packet should update the bytes count. |
| 2626 | probing_packet = ConstructProbingPacket(); |
| 2627 | received.reset(ConstructReceivedPacket( |
| 2628 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2629 | probing_packet->encrypted_length), |
| 2630 | clock_.Now())); |
| 2631 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2632 | |
| 2633 | EXPECT_EQ(num_probing_received + 2, |
| 2634 | connection_.GetStats().num_connectivity_probing_received); |
| 2635 | EXPECT_EQ(2 * bytes_sent, |
| 2636 | QuicConnectionPeer::BytesSentOnAlternativePath(&connection_)); |
| 2637 | EXPECT_EQ(2 * received->length(), |
| 2638 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 2639 | |
| 2640 | bool success = false; |
| 2641 | if (!connection_.validate_client_address()) { |
| 2642 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2643 | .Times(AtLeast(1u)) |
| 2644 | .WillOnce(Invoke([&]() { |
| 2645 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 2646 | payload = writer_->path_challenge_frames().front().data_buffer; |
| 2647 | })); |
| 2648 | |
| 2649 | connection_.ValidatePath( |
| 2650 | std::make_unique<TestQuicPathValidationContext>( |
| 2651 | connection_.self_address(), kNewPeerAddress, writer_.get()), |
| 2652 | std::make_unique<TestValidationResultDelegate>( |
| 2653 | &connection_, connection_.self_address(), kNewPeerAddress, |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 2654 | &success), |
| 2655 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2656 | } |
| 2657 | EXPECT_EQ((connection_.validate_client_address() ? 2 : 3) * bytes_sent, |
| 2658 | QuicConnectionPeer::BytesSentOnAlternativePath(&connection_)); |
| 2659 | QuicFrames frames; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 2660 | frames.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2661 | ProcessFramesPacketWithAddresses(frames, connection_.self_address(), |
| 2662 | kNewPeerAddress, |
| 2663 | ENCRYPTION_FORWARD_SECURE); |
| 2664 | EXPECT_LT(2 * received->length(), |
| 2665 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 2666 | if (connection_.validate_client_address()) { |
| 2667 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePathValidated(&connection_)); |
| 2668 | } |
| 2669 | // Receiving another probing packet from a newer address with a different |
| 2670 | // port shouldn't trigger another reverse path validation. |
| 2671 | QuicSocketAddress kNewerPeerAddress(QuicIpAddress::Loopback4(), |
| 2672 | /*port=*/34567); |
| 2673 | probing_packet = ConstructProbingPacket(); |
| 2674 | received.reset(ConstructReceivedPacket( |
| 2675 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2676 | probing_packet->encrypted_length), |
| 2677 | clock_.Now())); |
| 2678 | ProcessReceivedPacket(kSelfAddress, kNewerPeerAddress, *received); |
| 2679 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 2680 | EXPECT_EQ(connection_.validate_client_address(), |
| 2681 | QuicConnectionPeer::IsAlternativePathValidated(&connection_)); |
| 2682 | } |
| 2683 | |
| 2684 | // Process another packet with the old peer address on server side will not |
| 2685 | // start peer migration. |
| 2686 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2687 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 2688 | ENCRYPTION_INITIAL); |
| 2689 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2690 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2691 | } |
| 2692 | |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 2693 | // Receive a packet contains PATH CHALLENGE with self address change. |
| 2694 | TEST_P(QuicConnectionTest, ReceivePathProbingToPreferredAddressAtServer) { |
| 2695 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2696 | return; |
| 2697 | } |
| 2698 | ServerHandlePreferredAddressInit(); |
| 2699 | |
| 2700 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2701 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2702 | |
| 2703 | // Process a probing packet to the server preferred address. |
| 2704 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2705 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2706 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2707 | probing_packet->encrypted_length), |
| 2708 | clock_.Now())); |
| 2709 | uint64_t num_probing_received = |
| 2710 | connection_.GetStats().num_connectivity_probing_received; |
| 2711 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2712 | .Times(AtLeast(1u)) |
| 2713 | .WillOnce(Invoke([&]() { |
| 2714 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 2715 | // Verify that the PATH_RESPONSE is sent from the original self address. |
| 2716 | EXPECT_EQ(kSelfAddress.host(), writer_->last_write_source_address()); |
| 2717 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 2718 | })); |
| 2719 | ProcessReceivedPacket(kServerPreferredAddress, kPeerAddress, *received); |
| 2720 | |
| 2721 | EXPECT_EQ(num_probing_received + 1, |
| 2722 | connection_.GetStats().num_connectivity_probing_received); |
| 2723 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 2724 | &connection_, kServerPreferredAddress, kPeerAddress)); |
| 2725 | EXPECT_NE(kServerPreferredAddress, connection_.self_address()); |
| 2726 | |
| 2727 | // Receiving another probing packet from a new client address. |
| 2728 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 2729 | /*port=*/34567); |
| 2730 | probing_packet = ConstructProbingPacket(); |
| 2731 | received.reset(ConstructReceivedPacket( |
| 2732 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2733 | probing_packet->encrypted_length), |
| 2734 | clock_.Now())); |
| 2735 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2736 | .Times(AtLeast(1u)) |
| 2737 | .WillOnce(Invoke([&]() { |
| 2738 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 2739 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
fayang | 1f578d3 | 2023-03-20 11:36:15 -0700 | [diff] [blame] | 2740 | EXPECT_EQ(kServerPreferredAddress.host(), |
| 2741 | writer_->last_write_source_address()); |
| 2742 | // The responses should be sent from preferred address given server |
| 2743 | // has not received packet on original address from the new client |
| 2744 | // address. |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 2745 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 2746 | })); |
| 2747 | ProcessReceivedPacket(kServerPreferredAddress, kNewPeerAddress, *received); |
| 2748 | |
| 2749 | EXPECT_EQ(num_probing_received + 2, |
| 2750 | connection_.GetStats().num_connectivity_probing_received); |
| 2751 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath(&connection_, kSelfAddress, |
| 2752 | kNewPeerAddress)); |
| 2753 | EXPECT_LT(0u, QuicConnectionPeer::BytesSentOnAlternativePath(&connection_)); |
| 2754 | EXPECT_EQ(received->length(), |
| 2755 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 2756 | } |
| 2757 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2758 | // Receive a padded PING packet with a port change on server side. |
| 2759 | TEST_P(QuicConnectionTest, ReceivePaddedPingWithPortChangeAtServer) { |
| 2760 | set_perspective(Perspective::IS_SERVER); |
| 2761 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2762 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2763 | if (version().SupportsAntiAmplificationLimit()) { |
| 2764 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2765 | } |
| 2766 | |
| 2767 | // Clear direct_peer_address. |
| 2768 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2769 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2770 | // this test. |
| 2771 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2772 | QuicSocketAddress()); |
| 2773 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2774 | |
| 2775 | if (GetParam().version.UsesCryptoFrames()) { |
| 2776 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2777 | } else { |
| 2778 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2779 | } |
| 2780 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 2781 | ENCRYPTION_INITIAL); |
| 2782 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2783 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2784 | |
| 2785 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2786 | // In IETF version, a padded PING packet with port change is not taken as |
| 2787 | // connectivity probe. |
| 2788 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 2789 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 2790 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2791 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2792 | } else { |
| 2793 | // In non-IETF version, process a padded PING packet from a new peer |
| 2794 | // address on server side is effectively receiving a connectivity probing. |
| 2795 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2796 | EXPECT_CALL(visitor_, |
| 2797 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2798 | .Times(1); |
| 2799 | } |
| 2800 | const QuicSocketAddress kNewPeerAddress = |
| 2801 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2802 | |
| 2803 | QuicFrames frames; |
| 2804 | // Write a PING frame, which has no data payload. |
| 2805 | QuicPingFrame ping_frame; |
| 2806 | frames.push_back(QuicFrame(ping_frame)); |
| 2807 | |
| 2808 | // Add padding to the rest of the packet. |
| 2809 | QuicPaddingFrame padding_frame; |
| 2810 | frames.push_back(QuicFrame(padding_frame)); |
| 2811 | |
| 2812 | uint64_t num_probing_received = |
| 2813 | connection_.GetStats().num_connectivity_probing_received; |
| 2814 | |
| 2815 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 2816 | ENCRYPTION_INITIAL); |
| 2817 | |
| 2818 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2819 | // Padded PING with port changen is not considered as connectivity probe but |
| 2820 | // a PORT CHANGE. |
| 2821 | EXPECT_EQ(num_probing_received, |
| 2822 | connection_.GetStats().num_connectivity_probing_received); |
| 2823 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2824 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2825 | } else { |
| 2826 | EXPECT_EQ(num_probing_received + 1, |
| 2827 | connection_.GetStats().num_connectivity_probing_received); |
| 2828 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2829 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2830 | } |
| 2831 | |
| 2832 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2833 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2834 | } |
| 2835 | // Process another packet with the old peer address on server side. gQUIC |
| 2836 | // shouldn't regard this as a peer migration. |
| 2837 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 2838 | ENCRYPTION_INITIAL); |
| 2839 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2840 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2841 | } |
| 2842 | |
| 2843 | TEST_P(QuicConnectionTest, ReceiveReorderedPathProbingAtServer) { |
| 2844 | PathProbeTestInit(Perspective::IS_SERVER); |
| 2845 | |
| 2846 | // Decrease packet number to simulate out-of-order packets. |
| 2847 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 4); |
| 2848 | |
| 2849 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2850 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2851 | EXPECT_CALL(visitor_, |
| 2852 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2853 | .Times(1); |
| 2854 | } else { |
| 2855 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2856 | } |
| 2857 | |
| 2858 | // Process a padded PING packet from a new peer address on server side |
| 2859 | // is effectively receiving a connectivity probing, even if a newer packet has |
| 2860 | // been received before this one. |
| 2861 | const QuicSocketAddress kNewPeerAddress = |
| 2862 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2863 | |
| 2864 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2865 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2866 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2867 | probing_packet->encrypted_length), |
| 2868 | clock_.Now())); |
| 2869 | |
| 2870 | uint64_t num_probing_received = |
| 2871 | connection_.GetStats().num_connectivity_probing_received; |
| 2872 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2873 | |
| 2874 | EXPECT_EQ(num_probing_received + 1, |
| 2875 | connection_.GetStats().num_connectivity_probing_received); |
| 2876 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2877 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2878 | } |
| 2879 | |
| 2880 | TEST_P(QuicConnectionTest, MigrateAfterProbingAtServer) { |
| 2881 | PathProbeTestInit(Perspective::IS_SERVER); |
| 2882 | |
| 2883 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2884 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2885 | EXPECT_CALL(visitor_, |
| 2886 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2887 | .Times(1); |
| 2888 | } else { |
| 2889 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2890 | } |
| 2891 | |
| 2892 | // Process a padded PING packet from a new peer address on server side |
| 2893 | // is effectively receiving a connectivity probing. |
| 2894 | const QuicSocketAddress kNewPeerAddress = |
| 2895 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2896 | |
| 2897 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2898 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2899 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2900 | probing_packet->encrypted_length), |
| 2901 | clock_.Now())); |
| 2902 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2903 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2904 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2905 | |
| 2906 | // Process another non-probing packet with the new peer address on server |
| 2907 | // side will start peer migration. |
| 2908 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2909 | |
| 2910 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2911 | kNewPeerAddress, ENCRYPTION_INITIAL); |
| 2912 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2913 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2914 | } |
| 2915 | |
| 2916 | TEST_P(QuicConnectionTest, ReceiveConnectivityProbingPacketAtClient) { |
| 2917 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2918 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 2919 | |
| 2920 | // Client takes all padded PING packet as speculative connectivity |
| 2921 | // probing packet, and reports to visitor. |
| 2922 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2923 | |
| 2924 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2925 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2926 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2927 | probing_packet->encrypted_length), |
| 2928 | clock_.Now())); |
| 2929 | uint64_t num_probing_received = |
| 2930 | connection_.GetStats().num_connectivity_probing_received; |
| 2931 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, *received); |
| 2932 | |
| 2933 | EXPECT_EQ( |
| 2934 | num_probing_received + (GetParam().version.HasIetfQuicFrames() ? 1u : 0u), |
| 2935 | connection_.GetStats().num_connectivity_probing_received); |
| 2936 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2937 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2938 | } |
| 2939 | |
| 2940 | TEST_P(QuicConnectionTest, ReceiveConnectivityProbingResponseAtClient) { |
| 2941 | // TODO(b/150095484): add test coverage for IETF to verify that client takes |
| 2942 | // PATH RESPONSE with peer address change as correct validation on the new |
| 2943 | // path. |
| 2944 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2945 | return; |
| 2946 | } |
| 2947 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2948 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 2949 | |
| 2950 | // Process a padded PING packet with a different self address on client side |
| 2951 | // is effectively receiving a connectivity probing. |
| 2952 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2953 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2954 | EXPECT_CALL(visitor_, |
| 2955 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2956 | .Times(1); |
| 2957 | } else { |
| 2958 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2959 | } |
| 2960 | |
| 2961 | const QuicSocketAddress kNewSelfAddress = |
| 2962 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2963 | |
| 2964 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 2965 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2966 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2967 | probing_packet->encrypted_length), |
| 2968 | clock_.Now())); |
| 2969 | uint64_t num_probing_received = |
| 2970 | connection_.GetStats().num_connectivity_probing_received; |
| 2971 | ProcessReceivedPacket(kNewSelfAddress, kPeerAddress, *received); |
| 2972 | |
| 2973 | EXPECT_EQ(num_probing_received + 1, |
| 2974 | connection_.GetStats().num_connectivity_probing_received); |
| 2975 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2976 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2977 | } |
| 2978 | |
| 2979 | TEST_P(QuicConnectionTest, PeerAddressChangeAtClient) { |
| 2980 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2981 | set_perspective(Perspective::IS_CLIENT); |
| 2982 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2983 | |
| 2984 | // Clear direct_peer_address. |
| 2985 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2986 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2987 | // this test. |
| 2988 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2989 | QuicSocketAddress()); |
| 2990 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2991 | |
fayang | d1f2c99 | 2022-12-21 10:34:55 -0800 | [diff] [blame] | 2992 | if (connection_.version().HasIetfQuicFrames()) { |
| 2993 | // Verify the 2nd packet from unknown server address gets dropped. |
| 2994 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 2995 | } else if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2996 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(2); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2997 | } else { |
fayang | d1f2c99 | 2022-12-21 10:34:55 -0800 | [diff] [blame] | 2998 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(2); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 2999 | } |
| 3000 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 3001 | ENCRYPTION_INITIAL); |
| 3002 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3003 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3004 | const QuicSocketAddress kNewPeerAddress = |
| 3005 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 3006 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 3007 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 3008 | kNewPeerAddress, ENCRYPTION_INITIAL); |
| 3009 | if (connection_.version().HasIetfQuicFrames()) { |
| 3010 | // IETF QUIC disallows server initiated address change. |
| 3011 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3012 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 3013 | } else { |
| 3014 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 3015 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 3016 | } |
| 3017 | } |
| 3018 | |
fayang | d1f2c99 | 2022-12-21 10:34:55 -0800 | [diff] [blame] | 3019 | TEST_P(QuicConnectionTest, ServerAddressChangesToKnownAddress) { |
| 3020 | if (!connection_.version().HasIetfQuicFrames()) { |
| 3021 | return; |
| 3022 | } |
| 3023 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3024 | set_perspective(Perspective::IS_CLIENT); |
| 3025 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 3026 | |
| 3027 | // Clear direct_peer_address. |
| 3028 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 3029 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 3030 | // this test. |
| 3031 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 3032 | QuicSocketAddress()); |
| 3033 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 3034 | |
| 3035 | // Verify all 3 packets get processed. |
| 3036 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(3); |
| 3037 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 3038 | ENCRYPTION_INITIAL); |
| 3039 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3040 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 3041 | |
| 3042 | // Process another packet with a different but known server address. |
| 3043 | const QuicSocketAddress kNewPeerAddress = |
| 3044 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 3045 | connection_.AddKnownServerAddress(kNewPeerAddress); |
| 3046 | EXPECT_CALL(visitor_, OnConnectionMigration(_)).Times(0); |
| 3047 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 3048 | kNewPeerAddress, ENCRYPTION_INITIAL); |
| 3049 | // Verify peer address does not change. |
| 3050 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3051 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 3052 | |
| 3053 | // Process 3rd packet from previous server address. |
| 3054 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 3055 | ENCRYPTION_INITIAL); |
| 3056 | // Verify peer address does not change. |
| 3057 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3058 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 3059 | } |
| 3060 | |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 3061 | TEST_P(QuicConnectionTest, |
| 3062 | PeerAddressChangesToPreferredAddressBeforeClientInitiates) { |
| 3063 | if (!version().HasIetfQuicFrames()) { |
| 3064 | return; |
| 3065 | } |
| 3066 | ASSERT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 3067 | ASSERT_TRUE(connection_.self_address().host().IsIPv6()); |
| 3068 | SetQuicReloadableFlag(quic_connection_migration_use_new_cid_v2, true); |
| 3069 | const QuicConnectionId connection_id = TestConnectionId(17); |
| 3070 | const StatelessResetToken reset_token = |
| 3071 | QuicUtils::GenerateStatelessResetToken(connection_id); |
| 3072 | |
| 3073 | connection_.CreateConnectionIdManager(); |
| 3074 | |
| 3075 | connection_.SendCryptoStreamData(); |
| 3076 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3077 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 3078 | QuicAckFrame frame = InitAckFrame(1); |
| 3079 | // Received ACK for packet 1. |
| 3080 | ProcessFramePacketAtLevel(1, QuicFrame(&frame), ENCRYPTION_INITIAL); |
| 3081 | // Discard INITIAL key. |
| 3082 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 3083 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3084 | |
| 3085 | QuicConfig config; |
fayang | 6aa0d53 | 2023-01-10 20:06:35 -0800 | [diff] [blame] | 3086 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM, kSPAD}); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 3087 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, |
| 3088 | kTestStatelessResetToken); |
| 3089 | QuicConfigPeer::SetReceivedAlternateServerAddress(&config, |
| 3090 | kServerPreferredAddress); |
| 3091 | QuicConfigPeer::SetPreferredAddressConnectionIdAndToken( |
| 3092 | &config, connection_id, reset_token); |
| 3093 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 3094 | connection_.SetFromConfig(config); |
| 3095 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 3096 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 3097 | ASSERT_TRUE( |
| 3098 | QuicConnectionPeer::GetReceivedServerPreferredAddress(&connection_) |
| 3099 | .IsInitialized()); |
| 3100 | EXPECT_EQ( |
| 3101 | kServerPreferredAddress, |
| 3102 | QuicConnectionPeer::GetReceivedServerPreferredAddress(&connection_)); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 3103 | |
| 3104 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(0); |
| 3105 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 3106 | kServerPreferredAddress, ENCRYPTION_INITIAL); |
| 3107 | } |
| 3108 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3109 | TEST_P(QuicConnectionTest, MaxPacketSize) { |
| 3110 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 3111 | EXPECT_EQ(1250u, connection_.max_packet_length()); |
| 3112 | } |
| 3113 | |
| 3114 | TEST_P(QuicConnectionTest, PeerLowersMaxPacketSize) { |
| 3115 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 3116 | |
| 3117 | // SetFromConfig is always called after construction from InitializeSession. |
| 3118 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 3119 | constexpr uint32_t kTestMaxPacketSize = 1233u; |
| 3120 | QuicConfig config; |
| 3121 | QuicConfigPeer::SetReceivedMaxPacketSize(&config, kTestMaxPacketSize); |
| 3122 | connection_.SetFromConfig(config); |
| 3123 | |
| 3124 | EXPECT_EQ(kTestMaxPacketSize, connection_.max_packet_length()); |
| 3125 | } |
| 3126 | |
| 3127 | TEST_P(QuicConnectionTest, PeerCannotRaiseMaxPacketSize) { |
| 3128 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 3129 | |
| 3130 | // SetFromConfig is always called after construction from InitializeSession. |
| 3131 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 3132 | constexpr uint32_t kTestMaxPacketSize = 1450u; |
| 3133 | QuicConfig config; |
| 3134 | QuicConfigPeer::SetReceivedMaxPacketSize(&config, kTestMaxPacketSize); |
| 3135 | connection_.SetFromConfig(config); |
| 3136 | |
| 3137 | EXPECT_EQ(kDefaultMaxPacketSize, connection_.max_packet_length()); |
| 3138 | } |
| 3139 | |
| 3140 | TEST_P(QuicConnectionTest, SmallerServerMaxPacketSize) { |
| 3141 | TestConnection connection(TestConnectionId(), kSelfAddress, kPeerAddress, |
| 3142 | helper_.get(), alarm_factory_.get(), writer_.get(), |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 3143 | Perspective::IS_SERVER, version(), |
| 3144 | connection_id_generator_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3145 | EXPECT_EQ(Perspective::IS_SERVER, connection.perspective()); |
| 3146 | EXPECT_EQ(1000u, connection.max_packet_length()); |
| 3147 | } |
| 3148 | |
| 3149 | TEST_P(QuicConnectionTest, LowerServerResponseMtuTest) { |
| 3150 | set_perspective(Perspective::IS_SERVER); |
| 3151 | connection_.SetMaxPacketLength(1000); |
| 3152 | EXPECT_EQ(1000u, connection_.max_packet_length()); |
| 3153 | |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 3154 | SetQuicFlag(quic_use_lower_server_response_mtu_for_test, true); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3155 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(::testing::AtMost(1)); |
| 3156 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(::testing::AtMost(1)); |
| 3157 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 3158 | EXPECT_EQ(1250u, connection_.max_packet_length()); |
| 3159 | } |
| 3160 | |
| 3161 | TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSize) { |
| 3162 | set_perspective(Perspective::IS_SERVER); |
| 3163 | connection_.SetMaxPacketLength(1000); |
| 3164 | |
| 3165 | QuicPacketHeader header; |
| 3166 | header.destination_connection_id = connection_id_; |
| 3167 | header.version_flag = true; |
| 3168 | header.packet_number = QuicPacketNumber(12); |
| 3169 | |
| 3170 | if (QuicVersionHasLongHeaderLengths( |
| 3171 | peer_framer_.version().transport_version)) { |
| 3172 | header.long_packet_type = INITIAL; |
dschinazi | 35c0ff7 | 2022-08-16 12:10:06 -0700 | [diff] [blame] | 3173 | header.retry_token_length_length = quiche::VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 3174 | header.length_length = quiche::VARIABLE_LENGTH_INTEGER_LENGTH_2; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | QuicFrames frames; |
| 3178 | QuicPaddingFrame padding; |
| 3179 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3180 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 3181 | } else { |
| 3182 | frames.push_back(QuicFrame(frame1_)); |
| 3183 | } |
| 3184 | frames.push_back(QuicFrame(padding)); |
| 3185 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 3186 | char buffer[kMaxOutgoingPacketSize]; |
| 3187 | size_t encrypted_length = |
| 3188 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(12), |
| 3189 | *packet, buffer, kMaxOutgoingPacketSize); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 3190 | EXPECT_EQ(kMaxOutgoingPacketSize, |
| 3191 | encrypted_length + |
| 3192 | (connection_.version().KnowsWhichDecrypterToUse() ? 0 : 4)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3193 | |
| 3194 | framer_.set_version(version()); |
| 3195 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3196 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 3197 | } else { |
| 3198 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3199 | } |
| 3200 | connection_.ProcessUdpPacket( |
| 3201 | kSelfAddress, kPeerAddress, |
| 3202 | QuicReceivedPacket(buffer, encrypted_length, clock_.ApproximateNow(), |
| 3203 | false)); |
| 3204 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 3205 | EXPECT_EQ(kMaxOutgoingPacketSize, |
| 3206 | connection_.max_packet_length() + |
| 3207 | (connection_.version().KnowsWhichDecrypterToUse() ? 0 : 4)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSizeWhileWriterLimited) { |
| 3211 | const QuicByteCount lower_max_packet_size = 1240; |
| 3212 | writer_->set_max_packet_size(lower_max_packet_size); |
| 3213 | set_perspective(Perspective::IS_SERVER); |
| 3214 | connection_.SetMaxPacketLength(1000); |
| 3215 | EXPECT_EQ(1000u, connection_.max_packet_length()); |
| 3216 | |
| 3217 | QuicPacketHeader header; |
| 3218 | header.destination_connection_id = connection_id_; |
| 3219 | header.version_flag = true; |
| 3220 | header.packet_number = QuicPacketNumber(12); |
| 3221 | |
| 3222 | if (QuicVersionHasLongHeaderLengths( |
| 3223 | peer_framer_.version().transport_version)) { |
| 3224 | header.long_packet_type = INITIAL; |
dschinazi | 35c0ff7 | 2022-08-16 12:10:06 -0700 | [diff] [blame] | 3225 | header.retry_token_length_length = quiche::VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 3226 | header.length_length = quiche::VARIABLE_LENGTH_INTEGER_LENGTH_2; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | QuicFrames frames; |
| 3230 | QuicPaddingFrame padding; |
| 3231 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3232 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 3233 | } else { |
| 3234 | frames.push_back(QuicFrame(frame1_)); |
| 3235 | } |
| 3236 | frames.push_back(QuicFrame(padding)); |
| 3237 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 3238 | char buffer[kMaxOutgoingPacketSize]; |
| 3239 | size_t encrypted_length = |
| 3240 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(12), |
| 3241 | *packet, buffer, kMaxOutgoingPacketSize); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 3242 | EXPECT_EQ(kMaxOutgoingPacketSize, |
| 3243 | encrypted_length + |
| 3244 | (connection_.version().KnowsWhichDecrypterToUse() ? 0 : 4)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3245 | |
| 3246 | framer_.set_version(version()); |
| 3247 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3248 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 3249 | } else { |
| 3250 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3251 | } |
| 3252 | connection_.ProcessUdpPacket( |
| 3253 | kSelfAddress, kPeerAddress, |
| 3254 | QuicReceivedPacket(buffer, encrypted_length, clock_.ApproximateNow(), |
| 3255 | false)); |
| 3256 | |
| 3257 | // Here, the limit imposed by the writer is lower than the size of the packet |
| 3258 | // received, so the writer max packet size is used. |
| 3259 | EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); |
| 3260 | } |
| 3261 | |
| 3262 | TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriter) { |
| 3263 | const QuicByteCount lower_max_packet_size = 1240; |
| 3264 | writer_->set_max_packet_size(lower_max_packet_size); |
| 3265 | |
| 3266 | static_assert(lower_max_packet_size < kDefaultMaxPacketSize, |
| 3267 | "Default maximum packet size is too low"); |
| 3268 | connection_.SetMaxPacketLength(kDefaultMaxPacketSize); |
| 3269 | |
| 3270 | EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); |
| 3271 | } |
| 3272 | |
| 3273 | TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriterForNewConnection) { |
| 3274 | const QuicConnectionId connection_id = TestConnectionId(17); |
| 3275 | const QuicByteCount lower_max_packet_size = 1240; |
| 3276 | writer_->set_max_packet_size(lower_max_packet_size); |
| 3277 | TestConnection connection(connection_id, kSelfAddress, kPeerAddress, |
| 3278 | helper_.get(), alarm_factory_.get(), writer_.get(), |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 3279 | Perspective::IS_CLIENT, version(), |
| 3280 | connection_id_generator_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3281 | EXPECT_EQ(Perspective::IS_CLIENT, connection.perspective()); |
| 3282 | EXPECT_EQ(lower_max_packet_size, connection.max_packet_length()); |
| 3283 | } |
| 3284 | |
| 3285 | TEST_P(QuicConnectionTest, PacketsInOrder) { |
| 3286 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3287 | |
| 3288 | ProcessPacket(1); |
| 3289 | EXPECT_EQ(QuicPacketNumber(1u), LargestAcked(connection_.ack_frame())); |
| 3290 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 3291 | |
| 3292 | ProcessPacket(2); |
| 3293 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(connection_.ack_frame())); |
| 3294 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 3295 | |
| 3296 | ProcessPacket(3); |
| 3297 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3298 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 3299 | } |
| 3300 | |
| 3301 | TEST_P(QuicConnectionTest, PacketsOutOfOrder) { |
| 3302 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3303 | |
| 3304 | ProcessPacket(3); |
| 3305 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3306 | EXPECT_TRUE(IsMissing(2)); |
| 3307 | EXPECT_TRUE(IsMissing(1)); |
| 3308 | |
| 3309 | ProcessPacket(2); |
| 3310 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3311 | EXPECT_FALSE(IsMissing(2)); |
| 3312 | EXPECT_TRUE(IsMissing(1)); |
| 3313 | |
| 3314 | ProcessPacket(1); |
| 3315 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3316 | EXPECT_FALSE(IsMissing(2)); |
| 3317 | EXPECT_FALSE(IsMissing(1)); |
| 3318 | } |
| 3319 | |
| 3320 | TEST_P(QuicConnectionTest, DuplicatePacket) { |
| 3321 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3322 | |
| 3323 | ProcessPacket(3); |
| 3324 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3325 | EXPECT_TRUE(IsMissing(2)); |
| 3326 | EXPECT_TRUE(IsMissing(1)); |
| 3327 | |
| 3328 | // Send packet 3 again, but do not set the expectation that |
| 3329 | // the visitor OnStreamFrame() will be called. |
| 3330 | ProcessDataPacket(3); |
| 3331 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3332 | EXPECT_TRUE(IsMissing(2)); |
| 3333 | EXPECT_TRUE(IsMissing(1)); |
| 3334 | } |
| 3335 | |
| 3336 | TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) { |
| 3337 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 3338 | return; |
| 3339 | } |
| 3340 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3341 | |
| 3342 | ProcessPacket(3); |
| 3343 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3344 | EXPECT_TRUE(IsMissing(2)); |
| 3345 | EXPECT_TRUE(IsMissing(1)); |
| 3346 | |
| 3347 | ProcessPacket(2); |
| 3348 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 3349 | EXPECT_TRUE(IsMissing(1)); |
| 3350 | |
| 3351 | ProcessPacket(5); |
| 3352 | EXPECT_EQ(QuicPacketNumber(5u), LargestAcked(connection_.ack_frame())); |
| 3353 | EXPECT_TRUE(IsMissing(1)); |
| 3354 | EXPECT_TRUE(IsMissing(4)); |
| 3355 | |
| 3356 | // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a |
| 3357 | // packet the peer will not retransmit. It indicates this by sending 'least |
| 3358 | // awaiting' is 4. The connection should then realize 1 will not be |
| 3359 | // retransmitted, and will remove it from the missing list. |
| 3360 | QuicAckFrame frame = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3361 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3362 | ProcessAckPacket(6, &frame); |
| 3363 | |
| 3364 | // Force an ack to be sent. |
| 3365 | SendAckPacketToPeer(); |
| 3366 | EXPECT_TRUE(IsMissing(4)); |
| 3367 | } |
| 3368 | |
| 3369 | TEST_P(QuicConnectionTest, RejectUnencryptedStreamData) { |
| 3370 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 3371 | if (!IsDefaultTestConfiguration() || |
| 3372 | VersionHasIetfQuicFrames(version().transport_version)) { |
| 3373 | return; |
| 3374 | } |
| 3375 | |
| 3376 | // Process an unencrypted packet from the non-crypto stream. |
| 3377 | frame1_.stream_id = 3; |
| 3378 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3379 | EXPECT_CALL(visitor_, |
| 3380 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 3381 | EXPECT_QUIC_PEER_BUG(ProcessDataPacketAtLevel(1, false, ENCRYPTION_INITIAL), |
| 3382 | ""); |
| 3383 | TestConnectionCloseQuicErrorCode(QUIC_UNENCRYPTED_STREAM_DATA); |
| 3384 | } |
| 3385 | |
| 3386 | TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) { |
| 3387 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3388 | |
| 3389 | ProcessPacket(3); |
| 3390 | // Should not cause an ack. |
| 3391 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 3392 | |
| 3393 | ProcessPacket(2); |
| 3394 | // Should ack immediately, since this fills the last hole. |
| 3395 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 3396 | |
| 3397 | ProcessPacket(1); |
| 3398 | // Should ack immediately, since this fills the last hole. |
| 3399 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 3400 | |
| 3401 | ProcessPacket(4); |
| 3402 | // Should not cause an ack. |
| 3403 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 3404 | } |
| 3405 | |
| 3406 | TEST_P(QuicConnectionTest, OutOfOrderAckReceiptCausesNoAck) { |
| 3407 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3408 | |
| 3409 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 3410 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); |
| 3411 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 3412 | |
| 3413 | QuicAckFrame ack1 = InitAckFrame(1); |
| 3414 | QuicAckFrame ack2 = InitAckFrame(2); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3415 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3416 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 3417 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()).Times(1); |
| 3418 | } |
| 3419 | ProcessAckPacket(2, &ack2); |
| 3420 | // Should ack immediately since we have missing packets. |
| 3421 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 3422 | |
| 3423 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 3424 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()).Times(0); |
| 3425 | } |
| 3426 | ProcessAckPacket(1, &ack1); |
| 3427 | // Should not ack an ack filling a missing packet. |
| 3428 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 3429 | } |
| 3430 | |
| 3431 | TEST_P(QuicConnectionTest, AckReceiptCausesAckSend) { |
| 3432 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3433 | QuicPacketNumber original, second; |
| 3434 | |
| 3435 | QuicByteCount packet_size = |
| 3436 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &original); // 1st packet. |
| 3437 | SendStreamDataToPeer(3, "bar", 3, NO_FIN, &second); // 2nd packet. |
| 3438 | |
| 3439 | QuicAckFrame frame = InitAckFrame({{second, second + 1}}); |
| 3440 | // First nack triggers early retransmit. |
| 3441 | LostPacketVector lost_packets; |
| 3442 | lost_packets.push_back(LostPacket(original, kMaxOutgoingPacketSize)); |
| 3443 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 3444 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3445 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3446 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3447 | QuicPacketNumber retransmission; |
| 3448 | // Packet 1 is short header for IETF QUIC because the encryption level |
| 3449 | // switched to ENCRYPTION_FORWARD_SECURE in SendStreamDataToPeer. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 3450 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, packet_size, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3451 | .WillOnce(SaveArg<2>(&retransmission)); |
| 3452 | |
| 3453 | ProcessAckPacket(&frame); |
| 3454 | |
| 3455 | QuicAckFrame frame2 = ConstructAckFrame(retransmission, original); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3456 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3457 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 3458 | ProcessAckPacket(&frame2); |
| 3459 | |
| 3460 | // Now if the peer sends an ack which still reports the retransmitted packet |
| 3461 | // as missing, that will bundle an ack with data after two acks in a row |
| 3462 | // indicate the high water mark needs to be raised. |
| 3463 | EXPECT_CALL(*send_algorithm_, |
| 3464 | OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); |
| 3465 | connection_.SendStreamDataWithString(3, "foo", 6, NO_FIN); |
| 3466 | // No ack sent. |
| 3467 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3468 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 3469 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 3470 | |
| 3471 | // No more packet loss for the rest of the test. |
| 3472 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 3473 | .Times(AnyNumber()); |
| 3474 | ProcessAckPacket(&frame2); |
| 3475 | EXPECT_CALL(*send_algorithm_, |
| 3476 | OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); |
| 3477 | connection_.SendStreamDataWithString(3, "foofoofoo", 9, NO_FIN); |
| 3478 | // Ack bundled. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 3479 | // Do not ACK acks. |
| 3480 | EXPECT_EQ(1u, writer_->frame_count()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3481 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 3482 | EXPECT_TRUE(writer_->ack_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3483 | |
| 3484 | // But an ack with no missing packets will not send an ack. |
| 3485 | AckPacket(original, &frame2); |
| 3486 | ProcessAckPacket(&frame2); |
| 3487 | ProcessAckPacket(&frame2); |
| 3488 | } |
| 3489 | |
| 3490 | TEST_P(QuicConnectionTest, AckFrequencyUpdatedFromAckFrequencyFrame) { |
| 3491 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 3492 | return; |
| 3493 | } |
| 3494 | connection_.set_can_receive_ack_frequency_frame(); |
| 3495 | |
| 3496 | // Expect 13 acks, every 3rd packet including the first packet with |
| 3497 | // AckFrequencyFrame. |
| 3498 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(13); |
| 3499 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3500 | |
| 3501 | QuicAckFrequencyFrame ack_frequency_frame; |
| 3502 | ack_frequency_frame.packet_tolerance = 3; |
| 3503 | ProcessFramePacketAtLevel(1, QuicFrame(&ack_frequency_frame), |
| 3504 | ENCRYPTION_FORWARD_SECURE); |
| 3505 | |
| 3506 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(38); |
| 3507 | // Receives packets 2 - 39. |
| 3508 | for (size_t i = 2; i <= 39; ++i) { |
| 3509 | ProcessDataPacket(i); |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | TEST_P(QuicConnectionTest, AckDecimationReducesAcks) { |
| 3514 | const size_t kMinRttMs = 40; |
| 3515 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 3516 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 3517 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 3518 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 3519 | |
| 3520 | // Start ack decimation from 10th packet. |
| 3521 | connection_.set_min_received_before_ack_decimation(10); |
| 3522 | |
| 3523 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3524 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(30); |
| 3525 | |
| 3526 | // Expect 6 acks: 5 acks between packets 1-10, and ack at 20. |
| 3527 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6); |
| 3528 | // Receives packets 1 - 29. |
| 3529 | for (size_t i = 1; i <= 29; ++i) { |
| 3530 | ProcessDataPacket(i); |
| 3531 | } |
| 3532 | |
| 3533 | // We now receive the 30th packet, and so we send an ack. |
| 3534 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3535 | ProcessDataPacket(30); |
| 3536 | } |
| 3537 | |
| 3538 | TEST_P(QuicConnectionTest, AckNeedsRetransmittableFrames) { |
| 3539 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3540 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3541 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(99); |
| 3542 | |
| 3543 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(19); |
| 3544 | // Receives packets 1 - 39. |
| 3545 | for (size_t i = 1; i <= 39; ++i) { |
| 3546 | ProcessDataPacket(i); |
| 3547 | } |
| 3548 | // Receiving Packet 40 causes 20th ack to send. Session is informed and adds |
| 3549 | // WINDOW_UPDATE. |
| 3550 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3551 | .WillOnce(Invoke([this]() { |
| 3552 | connection_.SendControlFrame(QuicFrame(QuicWindowUpdateFrame(1, 0, 0))); |
| 3553 | })); |
| 3554 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3555 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3556 | ProcessDataPacket(40); |
| 3557 | EXPECT_EQ(1u, writer_->window_update_frames().size()); |
| 3558 | |
| 3559 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(9); |
| 3560 | // Receives packets 41 - 59. |
| 3561 | for (size_t i = 41; i <= 59; ++i) { |
| 3562 | ProcessDataPacket(i); |
| 3563 | } |
| 3564 | // Send a packet containing stream frame. |
| 3565 | SendStreamDataToPeer( |
| 3566 | QuicUtils::GetFirstBidirectionalStreamId( |
| 3567 | connection_.version().transport_version, Perspective::IS_CLIENT), |
| 3568 | "bar", 0, NO_FIN, nullptr); |
| 3569 | |
| 3570 | // Session will not be informed until receiving another 20 packets. |
| 3571 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(19); |
| 3572 | for (size_t i = 60; i <= 98; ++i) { |
| 3573 | ProcessDataPacket(i); |
| 3574 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3575 | } |
| 3576 | // Session does not add a retransmittable frame. |
| 3577 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3578 | .WillOnce(Invoke([this]() { |
| 3579 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 3580 | })); |
| 3581 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3582 | EXPECT_EQ(0u, writer_->ping_frames().size()); |
| 3583 | ProcessDataPacket(99); |
| 3584 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3585 | // A ping frame will be added. |
| 3586 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 3587 | } |
| 3588 | |
| 3589 | TEST_P(QuicConnectionTest, AckNeedsRetransmittableFramesAfterPto) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3590 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 3591 | QuicConfig config; |
| 3592 | QuicTagVector connection_options; |
| 3593 | connection_options.push_back(kEACK); |
| 3594 | config.SetConnectionOptionsToSend(connection_options); |
| 3595 | connection_.SetFromConfig(config); |
| 3596 | |
| 3597 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3598 | connection_.OnHandshakeComplete(); |
| 3599 | |
| 3600 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3601 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(10); |
| 3602 | |
| 3603 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(4); |
| 3604 | // Receive packets 1 - 9. |
| 3605 | for (size_t i = 1; i <= 9; ++i) { |
| 3606 | ProcessDataPacket(i); |
| 3607 | } |
| 3608 | |
| 3609 | // Send a ping and fire the retransmission alarm. |
| 3610 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 3611 | SendPing(); |
| 3612 | QuicTime retransmission_time = |
| 3613 | connection_.GetRetransmissionAlarm()->deadline(); |
| 3614 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 3615 | connection_.GetRetransmissionAlarm()->Fire(); |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 3616 | ASSERT_LT(0u, manager_->GetConsecutivePtoCount()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3617 | |
| 3618 | // Process a packet, which requests a retransmittable frame be bundled |
| 3619 | // with the ACK. |
| 3620 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3621 | .WillOnce(Invoke([this]() { |
| 3622 | connection_.SendControlFrame(QuicFrame(QuicWindowUpdateFrame(1, 0, 0))); |
| 3623 | })); |
| 3624 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3625 | ProcessDataPacket(11); |
| 3626 | EXPECT_EQ(1u, writer_->window_update_frames().size()); |
| 3627 | } |
| 3628 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3629 | TEST_P(QuicConnectionTest, TooManySentPackets) { |
| 3630 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3631 | |
| 3632 | QuicPacketCount max_tracked_packets = 50; |
| 3633 | QuicConnectionPeer::SetMaxTrackedPackets(&connection_, max_tracked_packets); |
| 3634 | |
| 3635 | const int num_packets = max_tracked_packets + 5; |
| 3636 | |
| 3637 | for (int i = 0; i < num_packets; ++i) { |
| 3638 | SendStreamDataToPeer(1, "foo", 3 * i, NO_FIN, nullptr); |
| 3639 | } |
| 3640 | |
| 3641 | EXPECT_CALL(visitor_, |
| 3642 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 3643 | |
| 3644 | ProcessFramePacket(QuicFrame(QuicPingFrame())); |
| 3645 | |
| 3646 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS); |
| 3647 | } |
| 3648 | |
| 3649 | TEST_P(QuicConnectionTest, LargestObservedLower) { |
| 3650 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3651 | |
| 3652 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 3653 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); |
| 3654 | SendStreamDataToPeer(1, "eep", 6, NO_FIN, nullptr); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3655 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3656 | |
| 3657 | // Start out saying the largest observed is 2. |
| 3658 | QuicAckFrame frame1 = InitAckFrame(1); |
| 3659 | QuicAckFrame frame2 = InitAckFrame(2); |
| 3660 | ProcessAckPacket(&frame2); |
| 3661 | |
| 3662 | EXPECT_CALL(visitor_, OnCanWrite()); |
| 3663 | ProcessAckPacket(&frame1); |
| 3664 | } |
| 3665 | |
| 3666 | TEST_P(QuicConnectionTest, AckUnsentData) { |
| 3667 | // Ack a packet which has not been sent. |
| 3668 | EXPECT_CALL(visitor_, |
| 3669 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 3670 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3671 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 3672 | QuicAckFrame frame = InitAckFrame(1); |
| 3673 | EXPECT_CALL(visitor_, OnCanWrite()).Times(0); |
| 3674 | ProcessAckPacket(&frame); |
| 3675 | TestConnectionCloseQuicErrorCode(QUIC_INVALID_ACK_DATA); |
| 3676 | } |
| 3677 | |
| 3678 | TEST_P(QuicConnectionTest, BasicSending) { |
| 3679 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 3680 | return; |
| 3681 | } |
| 3682 | const QuicConnectionStats& stats = connection_.GetStats(); |
| 3683 | EXPECT_FALSE(stats.first_decrypted_packet.IsInitialized()); |
| 3684 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3685 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3686 | ProcessDataPacket(1); |
| 3687 | EXPECT_EQ(QuicPacketNumber(1), stats.first_decrypted_packet); |
| 3688 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 3689 | QuicPacketNumber last_packet; |
| 3690 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 3691 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 3692 | SendAckPacketToPeer(); // Packet 2 |
| 3693 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3694 | SendAckPacketToPeer(); // Packet 3 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3695 | |
| 3696 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, &last_packet); // Packet 4 |
| 3697 | EXPECT_EQ(QuicPacketNumber(4u), last_packet); |
| 3698 | SendAckPacketToPeer(); // Packet 5 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3699 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3700 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3701 | |
| 3702 | // Peer acks up to packet 3. |
| 3703 | QuicAckFrame frame = InitAckFrame(3); |
| 3704 | ProcessAckPacket(&frame); |
| 3705 | SendAckPacketToPeer(); // Packet 6 |
| 3706 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 3707 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3708 | |
| 3709 | // Peer acks up to packet 4, the last packet. |
| 3710 | QuicAckFrame frame2 = InitAckFrame(6); |
| 3711 | ProcessAckPacket(&frame2); // Acks don't instigate acks. |
| 3712 | |
| 3713 | // Verify that we did not send an ack. |
| 3714 | EXPECT_EQ(QuicPacketNumber(6u), writer_->header().packet_number); |
| 3715 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3716 | // If we force an ack, we shouldn't change our retransmit state. |
| 3717 | SendAckPacketToPeer(); // Packet 7 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3718 | |
| 3719 | // But if we send more data it should. |
| 3720 | SendStreamDataToPeer(1, "eep", 6, NO_FIN, &last_packet); // Packet 8 |
| 3721 | EXPECT_EQ(QuicPacketNumber(8u), last_packet); |
| 3722 | SendAckPacketToPeer(); // Packet 9 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3723 | EXPECT_EQ(QuicPacketNumber(1), stats.first_decrypted_packet); |
| 3724 | } |
| 3725 | |
| 3726 | // QuicConnection should record the packet sent-time prior to sending the |
| 3727 | // packet. |
| 3728 | TEST_P(QuicConnectionTest, RecordSentTimeBeforePacketSent) { |
| 3729 | // We're using a MockClock for the tests, so we have complete control over the |
| 3730 | // time. |
| 3731 | // Our recorded timestamp for the last packet sent time will be passed in to |
| 3732 | // the send_algorithm. Make sure that it is set to the correct value. |
| 3733 | QuicTime actual_recorded_send_time = QuicTime::Zero(); |
| 3734 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3735 | .WillOnce(SaveArg<0>(&actual_recorded_send_time)); |
| 3736 | |
| 3737 | // First send without any pause and check the result. |
| 3738 | QuicTime expected_recorded_send_time = clock_.Now(); |
| 3739 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 3740 | EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 3741 | << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 3742 | << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 3743 | |
| 3744 | // Now pause during the write, and check the results. |
| 3745 | actual_recorded_send_time = QuicTime::Zero(); |
| 3746 | const QuicTime::Delta write_pause_time_delta = |
| 3747 | QuicTime::Delta::FromMilliseconds(5000); |
| 3748 | SetWritePauseTimeDelta(write_pause_time_delta); |
| 3749 | expected_recorded_send_time = clock_.Now(); |
| 3750 | |
| 3751 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3752 | .WillOnce(SaveArg<0>(&actual_recorded_send_time)); |
| 3753 | connection_.SendStreamDataWithString(2, "baz", 0, NO_FIN); |
| 3754 | EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 3755 | << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 3756 | << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 3757 | } |
| 3758 | |
QUICHE team | ac0a808 | 2022-06-13 09:17:05 -0700 | [diff] [blame] | 3759 | TEST_P(QuicConnectionTest, ConnectionStatsRetransmission_WithRetransmissions) { |
| 3760 | // Send two stream frames in 1 packet by queueing them. |
| 3761 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3762 | |
| 3763 | { |
| 3764 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3765 | connection_.SaveAndSendStreamData( |
| 3766 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3767 | "helloworld", 0, NO_FIN, PTO_RETRANSMISSION); |
| 3768 | connection_.SaveAndSendStreamData( |
| 3769 | GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3770 | "helloworld", 0, NO_FIN, LOSS_RETRANSMISSION); |
| 3771 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3772 | } |
| 3773 | |
| 3774 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3775 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3776 | |
| 3777 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3778 | for (auto& frame : writer_->stream_frames()) { |
| 3779 | EXPECT_EQ(frame->data_length, 10u); |
| 3780 | } |
| 3781 | |
| 3782 | ASSERT_EQ(connection_.GetStats().packets_retransmitted, 1u); |
| 3783 | ASSERT_GE(connection_.GetStats().bytes_retransmitted, 20u); |
| 3784 | } |
| 3785 | |
| 3786 | TEST_P(QuicConnectionTest, ConnectionStatsRetransmission_WithMixedFrames) { |
| 3787 | // Send two stream frames in 1 packet by queueing them. |
| 3788 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3789 | |
| 3790 | { |
| 3791 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3792 | // First frame is retransmission. Second is NOT_RETRANSMISSION but the |
| 3793 | // packet retains the PTO_RETRANSMISSION type. |
| 3794 | connection_.SaveAndSendStreamData( |
| 3795 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3796 | "helloworld", 0, NO_FIN, PTO_RETRANSMISSION); |
| 3797 | connection_.SaveAndSendStreamData( |
| 3798 | GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3799 | "helloworld", 0, NO_FIN, NOT_RETRANSMISSION); |
| 3800 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3801 | } |
| 3802 | |
| 3803 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3804 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3805 | |
| 3806 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3807 | for (auto& frame : writer_->stream_frames()) { |
| 3808 | EXPECT_EQ(frame->data_length, 10u); |
| 3809 | } |
| 3810 | |
| 3811 | ASSERT_EQ(connection_.GetStats().packets_retransmitted, 1u); |
| 3812 | ASSERT_GE(connection_.GetStats().bytes_retransmitted, 10u); |
| 3813 | } |
| 3814 | |
| 3815 | TEST_P(QuicConnectionTest, ConnectionStatsRetransmission_NoRetransmission) { |
| 3816 | // Send two stream frames in 1 packet by queueing them. |
| 3817 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3818 | |
| 3819 | { |
| 3820 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3821 | // Both frames are NOT_RETRANSMISSION |
| 3822 | connection_.SaveAndSendStreamData( |
| 3823 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3824 | "helloworld", 0, NO_FIN, NOT_RETRANSMISSION); |
| 3825 | connection_.SaveAndSendStreamData( |
| 3826 | GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3827 | "helloworld", 0, NO_FIN, NOT_RETRANSMISSION); |
| 3828 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3829 | } |
| 3830 | |
| 3831 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3832 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3833 | |
| 3834 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3835 | ASSERT_EQ(connection_.GetStats().packets_retransmitted, 0u); |
| 3836 | ASSERT_EQ(connection_.GetStats().bytes_retransmitted, 0u); |
| 3837 | } |
| 3838 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3839 | TEST_P(QuicConnectionTest, FramePacking) { |
| 3840 | // Send two stream frames in 1 packet by queueing them. |
| 3841 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3842 | { |
| 3843 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3844 | connection_.SendStreamData3(); |
| 3845 | connection_.SendStreamData5(); |
| 3846 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3847 | } |
| 3848 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3849 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3850 | |
| 3851 | // Parse the last packet and ensure it's an ack and two stream frames from |
| 3852 | // two different streams. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 3853 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3854 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3855 | |
| 3856 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 3857 | |
| 3858 | ASSERT_EQ(2u, writer_->stream_frames().size()); |
| 3859 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3860 | writer_->stream_frames()[0]->stream_id); |
| 3861 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3862 | writer_->stream_frames()[1]->stream_id); |
| 3863 | } |
| 3864 | |
| 3865 | TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) { |
| 3866 | // Send two stream frames (one non-crypto, then one crypto) in 2 packets by |
| 3867 | // queueing them. |
| 3868 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3869 | { |
| 3870 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 3871 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3872 | connection_.SendStreamData3(); |
| 3873 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 3874 | // Set the crypters for INITIAL packets in the TestPacketWriter. |
| 3875 | if (!connection_.version().KnowsWhichDecrypterToUse()) { |
| 3876 | writer_->framer()->framer()->SetAlternativeDecrypter( |
| 3877 | ENCRYPTION_INITIAL, |
| 3878 | std::make_unique<NullDecrypter>(Perspective::IS_SERVER), false); |
| 3879 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3880 | connection_.SendCryptoStreamData(); |
| 3881 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3882 | } |
| 3883 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3884 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3885 | |
| 3886 | // Parse the last packet and ensure it contains a crypto stream frame. |
| 3887 | EXPECT_LE(2u, writer_->frame_count()); |
| 3888 | ASSERT_LE(1u, writer_->padding_frames().size()); |
| 3889 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3890 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 3891 | EXPECT_EQ(QuicUtils::GetCryptoStreamId(connection_.transport_version()), |
| 3892 | writer_->stream_frames()[0]->stream_id); |
| 3893 | } else { |
| 3894 | EXPECT_LE(1u, writer_->crypto_frames().size()); |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) { |
| 3899 | // Send two stream frames (one crypto, then one non-crypto) in 2 packets by |
| 3900 | // queueing them. |
| 3901 | { |
| 3902 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3903 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 3904 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 3905 | connection_.SendCryptoStreamData(); |
| 3906 | connection_.SendStreamData3(); |
| 3907 | } |
| 3908 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3909 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3910 | |
| 3911 | // Parse the last packet and ensure it's the stream frame from stream 3. |
| 3912 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3913 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 3914 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 3915 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3916 | writer_->stream_frames()[0]->stream_id); |
| 3917 | } |
| 3918 | |
| 3919 | TEST_P(QuicConnectionTest, FramePackingAckResponse) { |
| 3920 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3921 | // Process a data packet to queue up a pending ack. |
| 3922 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3923 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 3924 | } else { |
| 3925 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3926 | } |
| 3927 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 3928 | |
| 3929 | QuicPacketNumber last_packet; |
| 3930 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3931 | connection_.SendCryptoDataWithString("foo", 0); |
| 3932 | } else { |
| 3933 | SendStreamDataToPeer( |
| 3934 | QuicUtils::GetCryptoStreamId(connection_.transport_version()), "foo", 0, |
| 3935 | NO_FIN, &last_packet); |
| 3936 | } |
| 3937 | // Verify ack is bundled with outging packet. |
| 3938 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 3939 | |
| 3940 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 3941 | .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( |
| 3942 | &connection_, &TestConnection::SendStreamData3)), |
| 3943 | IgnoreResult(InvokeWithoutArgs( |
| 3944 | &connection_, &TestConnection::SendStreamData5)))); |
| 3945 | |
| 3946 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3947 | |
| 3948 | // Process a data packet to cause the visitor's OnCanWrite to be invoked. |
| 3949 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 3950 | peer_framer_.SetEncrypter( |
| 3951 | ENCRYPTION_FORWARD_SECURE, |
| 3952 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
| 3953 | SetDecrypter( |
| 3954 | ENCRYPTION_FORWARD_SECURE, |
| 3955 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3956 | ProcessDataPacket(2); |
| 3957 | |
| 3958 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3959 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3960 | |
| 3961 | // Parse the last packet and ensure it's an ack and two stream frames from |
| 3962 | // two different streams. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 3963 | EXPECT_EQ(3u, writer_->frame_count()); |
| 3964 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 3965 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 3966 | ASSERT_EQ(2u, writer_->stream_frames().size()); |
| 3967 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3968 | writer_->stream_frames()[0]->stream_id); |
| 3969 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3970 | writer_->stream_frames()[1]->stream_id); |
| 3971 | } |
| 3972 | |
| 3973 | TEST_P(QuicConnectionTest, FramePackingSendv) { |
| 3974 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3975 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 3976 | |
| 3977 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 3978 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 3979 | connection_.SaveAndSendStreamData(stream_id, "ABCDEF", 0, NO_FIN); |
| 3980 | |
| 3981 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3982 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3983 | |
| 3984 | // Parse the last packet and ensure multiple iovector blocks have |
| 3985 | // been packed into a single stream frame from one stream. |
| 3986 | EXPECT_EQ(1u, writer_->frame_count()); |
| 3987 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 3988 | EXPECT_EQ(0u, writer_->padding_frames().size()); |
| 3989 | QuicStreamFrame* frame = writer_->stream_frames()[0].get(); |
| 3990 | EXPECT_EQ(stream_id, frame->stream_id); |
| 3991 | EXPECT_EQ("ABCDEF", |
| 3992 | absl::string_view(frame->data_buffer, frame->data_length)); |
| 3993 | } |
| 3994 | |
| 3995 | TEST_P(QuicConnectionTest, FramePackingSendvQueued) { |
| 3996 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3997 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 3998 | |
| 3999 | BlockOnNextWrite(); |
| 4000 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 4001 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 4002 | connection_.SaveAndSendStreamData(stream_id, "ABCDEF", 0, NO_FIN); |
| 4003 | |
| 4004 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4005 | EXPECT_TRUE(connection_.HasQueuedData()); |
| 4006 | |
| 4007 | // Unblock the writes and actually send. |
| 4008 | writer_->SetWritable(); |
| 4009 | connection_.OnCanWrite(); |
| 4010 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4011 | |
| 4012 | // Parse the last packet and ensure it's one stream frame from one stream. |
| 4013 | EXPECT_EQ(1u, writer_->frame_count()); |
| 4014 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 4015 | EXPECT_EQ(0u, writer_->padding_frames().size()); |
| 4016 | QuicStreamFrame* frame = writer_->stream_frames()[0].get(); |
| 4017 | EXPECT_EQ(stream_id, frame->stream_id); |
| 4018 | EXPECT_EQ("ABCDEF", |
| 4019 | absl::string_view(frame->data_buffer, frame->data_length)); |
| 4020 | } |
| 4021 | |
| 4022 | TEST_P(QuicConnectionTest, SendingZeroBytes) { |
| 4023 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 4024 | // Send a zero byte write with a fin using writev. |
| 4025 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 4026 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 4027 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 4028 | connection_.SaveAndSendStreamData(stream_id, {}, 0, FIN); |
| 4029 | |
| 4030 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4031 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 4032 | |
| 4033 | // Padding frames are added by v99 to ensure a minimum packet size. |
| 4034 | size_t extra_padding_frames = 0; |
| 4035 | if (GetParam().version.HasHeaderProtection()) { |
| 4036 | extra_padding_frames = 1; |
| 4037 | } |
| 4038 | |
| 4039 | // Parse the last packet and ensure it's one stream frame from one stream. |
| 4040 | EXPECT_EQ(1u + extra_padding_frames, writer_->frame_count()); |
| 4041 | EXPECT_EQ(extra_padding_frames, writer_->padding_frames().size()); |
| 4042 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 4043 | EXPECT_EQ(stream_id, writer_->stream_frames()[0]->stream_id); |
| 4044 | EXPECT_TRUE(writer_->stream_frames()[0]->fin); |
| 4045 | } |
| 4046 | |
| 4047 | TEST_P(QuicConnectionTest, LargeSendWithPendingAck) { |
| 4048 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 4049 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 4050 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 4051 | // Set the ack alarm by processing a ping frame. |
| 4052 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4053 | |
| 4054 | // Processs a PING frame. |
| 4055 | ProcessFramePacket(QuicFrame(QuicPingFrame())); |
| 4056 | // Ensure that this has caused the ACK alarm to be set. |
| 4057 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 4058 | |
| 4059 | // Send data and ensure the ack is bundled. |
| 4060 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(9); |
| 4061 | const std::string data(10000, '?'); |
| 4062 | QuicConsumedData consumed = connection_.SaveAndSendStreamData( |
| 4063 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), data, |
| 4064 | 0, FIN); |
| 4065 | EXPECT_EQ(data.length(), consumed.bytes_consumed); |
| 4066 | EXPECT_TRUE(consumed.fin_consumed); |
| 4067 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4068 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 4069 | |
| 4070 | // Parse the last packet and ensure it's one stream frame with a fin. |
| 4071 | EXPECT_EQ(1u, writer_->frame_count()); |
| 4072 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 4073 | EXPECT_EQ(GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 4074 | writer_->stream_frames()[0]->stream_id); |
| 4075 | EXPECT_TRUE(writer_->stream_frames()[0]->fin); |
| 4076 | // Ensure the ack alarm was cancelled when the ack was sent. |
| 4077 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 4078 | } |
| 4079 | |
| 4080 | TEST_P(QuicConnectionTest, OnCanWrite) { |
| 4081 | // Visitor's OnCanWrite will send data, but will have more pending writes. |
| 4082 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 4083 | .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( |
| 4084 | &connection_, &TestConnection::SendStreamData3)), |
| 4085 | IgnoreResult(InvokeWithoutArgs( |
| 4086 | &connection_, &TestConnection::SendStreamData5)))); |
| 4087 | { |
| 4088 | InSequence seq; |
| 4089 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillOnce(Return(true)); |
| 4090 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 4091 | .WillRepeatedly(Return(false)); |
| 4092 | } |
| 4093 | |
| 4094 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 4095 | .WillRepeatedly(testing::Return(true)); |
| 4096 | |
| 4097 | connection_.OnCanWrite(); |
| 4098 | |
| 4099 | // Parse the last packet and ensure it's the two stream frames from |
| 4100 | // two different streams. |
| 4101 | EXPECT_EQ(2u, writer_->frame_count()); |
| 4102 | EXPECT_EQ(2u, writer_->stream_frames().size()); |
| 4103 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 4104 | writer_->stream_frames()[0]->stream_id); |
| 4105 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 4106 | writer_->stream_frames()[1]->stream_id); |
| 4107 | } |
| 4108 | |
| 4109 | TEST_P(QuicConnectionTest, RetransmitOnNack) { |
| 4110 | QuicPacketNumber last_packet; |
| 4111 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); |
| 4112 | SendStreamDataToPeer(3, "foos", 3, NO_FIN, &last_packet); |
| 4113 | SendStreamDataToPeer(3, "fooos", 7, NO_FIN, &last_packet); |
| 4114 | |
| 4115 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4116 | |
| 4117 | // Don't lose a packet on an ack, and nothing is retransmitted. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4118 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4119 | QuicAckFrame ack_one = InitAckFrame(1); |
| 4120 | ProcessAckPacket(&ack_one); |
| 4121 | |
| 4122 | // Lose a packet and ensure it triggers retransmission. |
| 4123 | QuicAckFrame nack_two = ConstructAckFrame(3, 2); |
| 4124 | LostPacketVector lost_packets; |
| 4125 | lost_packets.push_back( |
| 4126 | LostPacket(QuicPacketNumber(2), kMaxOutgoingPacketSize)); |
| 4127 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4128 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4129 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4130 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4131 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4132 | EXPECT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(creator_)); |
| 4133 | ProcessAckPacket(&nack_two); |
| 4134 | } |
| 4135 | |
| 4136 | TEST_P(QuicConnectionTest, DoNotSendQueuedPacketForResetStream) { |
| 4137 | // Block the connection to queue the packet. |
| 4138 | BlockOnNextWrite(); |
| 4139 | |
| 4140 | QuicStreamId stream_id = 2; |
| 4141 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 4142 | |
| 4143 | // Now that there is a queued packet, reset the stream. |
| 4144 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 4145 | |
| 4146 | // Unblock the connection and verify that only the RST_STREAM is sent. |
| 4147 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4148 | writer_->SetWritable(); |
| 4149 | connection_.OnCanWrite(); |
| 4150 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4151 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 4152 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 4153 | } |
| 4154 | |
| 4155 | TEST_P(QuicConnectionTest, SendQueuedPacketForQuicRstStreamNoError) { |
| 4156 | // Block the connection to queue the packet. |
| 4157 | BlockOnNextWrite(); |
| 4158 | |
| 4159 | QuicStreamId stream_id = 2; |
| 4160 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4161 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 4162 | |
| 4163 | // Now that there is a queued packet, reset the stream. |
| 4164 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 3); |
| 4165 | |
| 4166 | // Unblock the connection and verify that the RST_STREAM is sent and the data |
| 4167 | // packet is sent. |
| 4168 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 4169 | writer_->SetWritable(); |
| 4170 | connection_.OnCanWrite(); |
| 4171 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4172 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 4173 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 4174 | } |
| 4175 | |
| 4176 | TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnNack) { |
| 4177 | QuicStreamId stream_id = 2; |
| 4178 | QuicPacketNumber last_packet; |
| 4179 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4180 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 4181 | SendStreamDataToPeer(stream_id, "fooos", 7, NO_FIN, &last_packet); |
| 4182 | |
| 4183 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4184 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 12); |
| 4185 | |
| 4186 | // Lose a packet and ensure it does not trigger retransmission. |
| 4187 | QuicAckFrame nack_two = ConstructAckFrame(last_packet, last_packet - 1); |
| 4188 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4189 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4190 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4191 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4192 | ProcessAckPacket(&nack_two); |
| 4193 | } |
| 4194 | |
| 4195 | TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnNack) { |
| 4196 | QuicStreamId stream_id = 2; |
| 4197 | QuicPacketNumber last_packet; |
| 4198 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4199 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 4200 | SendStreamDataToPeer(stream_id, "fooos", 7, NO_FIN, &last_packet); |
| 4201 | |
| 4202 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4203 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 12); |
| 4204 | |
| 4205 | // Lose a packet, ensure it triggers retransmission. |
| 4206 | QuicAckFrame nack_two = ConstructAckFrame(last_packet, last_packet - 1); |
| 4207 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4208 | LostPacketVector lost_packets; |
| 4209 | lost_packets.push_back(LostPacket(last_packet - 1, kMaxOutgoingPacketSize)); |
| 4210 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4211 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4212 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4213 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4214 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 4215 | ProcessAckPacket(&nack_two); |
| 4216 | } |
| 4217 | |
| 4218 | TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnRTO) { |
| 4219 | QuicStreamId stream_id = 2; |
| 4220 | QuicPacketNumber last_packet; |
| 4221 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4222 | |
| 4223 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4224 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 4225 | |
| 4226 | // Fire the RTO and verify that the RST_STREAM is resent, not stream data. |
| 4227 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4228 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4229 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4230 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4231 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 4232 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 4233 | EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); |
| 4234 | } |
| 4235 | |
| 4236 | // Ensure that if the only data in flight is non-retransmittable, the |
| 4237 | // retransmission alarm is not set. |
| 4238 | TEST_P(QuicConnectionTest, CancelRetransmissionAlarmAfterResetStream) { |
| 4239 | QuicStreamId stream_id = 2; |
| 4240 | QuicPacketNumber last_data_packet; |
| 4241 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 4242 | |
| 4243 | // Cancel the stream. |
| 4244 | const QuicPacketNumber rst_packet = last_data_packet + 1; |
| 4245 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, rst_packet, _, _)).Times(1); |
| 4246 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 4247 | |
| 4248 | // Ack the RST_STREAM frame (since it's retransmittable), but not the data |
| 4249 | // packet, which is no longer retransmittable since the stream was cancelled. |
| 4250 | QuicAckFrame nack_stream_data = |
| 4251 | ConstructAckFrame(rst_packet, last_data_packet); |
| 4252 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4253 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4254 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4255 | ProcessAckPacket(&nack_stream_data); |
| 4256 | |
| 4257 | // Ensure that the data is still in flight, but the retransmission alarm is no |
| 4258 | // longer set. |
| 4259 | EXPECT_GT(manager_->GetBytesInFlight(), 0u); |
| 4260 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4261 | } |
| 4262 | |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 4263 | TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnPTO) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4264 | QuicStreamId stream_id = 2; |
| 4265 | QuicPacketNumber last_packet; |
| 4266 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4267 | |
| 4268 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4269 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 3); |
| 4270 | |
| 4271 | // Fire the RTO and verify that the RST_STREAM is resent, the stream data |
| 4272 | // is sent. |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 4273 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4274 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4275 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4276 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4277 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | TEST_P(QuicConnectionTest, DoNotSendPendingRetransmissionForResetStream) { |
| 4281 | QuicStreamId stream_id = 2; |
| 4282 | QuicPacketNumber last_packet; |
| 4283 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4284 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 4285 | BlockOnNextWrite(); |
| 4286 | connection_.SendStreamDataWithString(stream_id, "fooos", 7, NO_FIN); |
| 4287 | |
| 4288 | // Lose a packet which will trigger a pending retransmission. |
| 4289 | QuicAckFrame ack = ConstructAckFrame(last_packet, last_packet - 1); |
| 4290 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4291 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4292 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4293 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4294 | ProcessAckPacket(&ack); |
| 4295 | |
| 4296 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 12); |
| 4297 | |
| 4298 | // Unblock the connection and verify that the RST_STREAM is sent but not the |
| 4299 | // second data packet nor a retransmit. |
| 4300 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4301 | writer_->SetWritable(); |
| 4302 | connection_.OnCanWrite(); |
| 4303 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4304 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 4305 | ASSERT_EQ(1u, writer_->rst_stream_frames().size()); |
| 4306 | EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); |
| 4307 | } |
| 4308 | |
| 4309 | TEST_P(QuicConnectionTest, SendPendingRetransmissionForQuicRstStreamNoError) { |
| 4310 | QuicStreamId stream_id = 2; |
| 4311 | QuicPacketNumber last_packet; |
| 4312 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 4313 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 4314 | BlockOnNextWrite(); |
| 4315 | connection_.SendStreamDataWithString(stream_id, "fooos", 7, NO_FIN); |
| 4316 | |
| 4317 | // Lose a packet which will trigger a pending retransmission. |
| 4318 | QuicAckFrame ack = ConstructAckFrame(last_packet, last_packet - 1); |
| 4319 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4320 | LostPacketVector lost_packets; |
| 4321 | lost_packets.push_back(LostPacket(last_packet - 1, kMaxOutgoingPacketSize)); |
| 4322 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4323 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4324 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4325 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4326 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4327 | ProcessAckPacket(&ack); |
| 4328 | |
| 4329 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 12); |
| 4330 | |
| 4331 | // Unblock the connection and verify that the RST_STREAM is sent and the |
| 4332 | // second data packet or a retransmit is sent. |
| 4333 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(2)); |
| 4334 | writer_->SetWritable(); |
| 4335 | connection_.OnCanWrite(); |
| 4336 | // The RST_STREAM_FRAME is sent after queued packets and pending |
| 4337 | // retransmission. |
| 4338 | connection_.SendControlFrame(QuicFrame( |
| 4339 | new QuicRstStreamFrame(1, stream_id, QUIC_STREAM_NO_ERROR, 14))); |
| 4340 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4341 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 4342 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 4343 | } |
| 4344 | |
| 4345 | TEST_P(QuicConnectionTest, RetransmitAckedPacket) { |
| 4346 | QuicPacketNumber last_packet; |
| 4347 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 4348 | SendStreamDataToPeer(1, "foos", 3, NO_FIN, &last_packet); // Packet 2 |
| 4349 | SendStreamDataToPeer(1, "fooos", 7, NO_FIN, &last_packet); // Packet 3 |
| 4350 | |
| 4351 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4352 | |
| 4353 | // Instigate a loss with an ack. |
| 4354 | QuicAckFrame nack_two = ConstructAckFrame(3, 2); |
| 4355 | // The first nack should trigger a fast retransmission, but we'll be |
| 4356 | // write blocked, so the packet will be queued. |
| 4357 | BlockOnNextWrite(); |
| 4358 | |
| 4359 | LostPacketVector lost_packets; |
| 4360 | lost_packets.push_back( |
| 4361 | LostPacket(QuicPacketNumber(2), kMaxOutgoingPacketSize)); |
| 4362 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4363 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4364 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4365 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4366 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(4), _, _)) |
| 4367 | .Times(1); |
| 4368 | ProcessAckPacket(&nack_two); |
| 4369 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4370 | |
| 4371 | // Now, ack the previous transmission. |
| 4372 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4373 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(false, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4374 | QuicAckFrame ack_all = InitAckFrame(3); |
| 4375 | ProcessAckPacket(&ack_all); |
| 4376 | |
| 4377 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(4), _, _)) |
| 4378 | .Times(0); |
| 4379 | |
| 4380 | writer_->SetWritable(); |
| 4381 | connection_.OnCanWrite(); |
| 4382 | |
| 4383 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4384 | // We do not store retransmittable frames of this retransmission. |
| 4385 | EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, 4)); |
| 4386 | } |
| 4387 | |
| 4388 | TEST_P(QuicConnectionTest, RetransmitNackedLargestObserved) { |
| 4389 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4390 | QuicPacketNumber original, second; |
| 4391 | |
| 4392 | QuicByteCount packet_size = |
| 4393 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &original); // 1st packet. |
| 4394 | SendStreamDataToPeer(3, "bar", 3, NO_FIN, &second); // 2nd packet. |
| 4395 | |
| 4396 | QuicAckFrame frame = InitAckFrame({{second, second + 1}}); |
| 4397 | // The first nack should retransmit the largest observed packet. |
| 4398 | LostPacketVector lost_packets; |
| 4399 | lost_packets.push_back(LostPacket(original, kMaxOutgoingPacketSize)); |
| 4400 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4401 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4402 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4403 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4404 | // Packet 1 is short header for IETF QUIC because the encryption level |
| 4405 | // switched to ENCRYPTION_FORWARD_SECURE in SendStreamDataToPeer. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 4406 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, packet_size, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4407 | ProcessAckPacket(&frame); |
| 4408 | } |
| 4409 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4410 | TEST_P(QuicConnectionTest, WriteBlockedBufferedThenSent) { |
| 4411 | BlockOnNextWrite(); |
| 4412 | writer_->set_is_write_blocked_data_buffered(true); |
| 4413 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4414 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4415 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4416 | |
| 4417 | writer_->SetWritable(); |
| 4418 | connection_.OnCanWrite(); |
| 4419 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4420 | } |
| 4421 | |
| 4422 | TEST_P(QuicConnectionTest, WriteBlockedThenSent) { |
| 4423 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4424 | BlockOnNextWrite(); |
| 4425 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4426 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4427 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4428 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4429 | |
| 4430 | // The second packet should also be queued, in order to ensure packets are |
| 4431 | // never sent out of order. |
| 4432 | writer_->SetWritable(); |
| 4433 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4434 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4435 | EXPECT_EQ(2u, connection_.NumQueuedPackets()); |
| 4436 | |
| 4437 | // Now both are sent in order when we unblock. |
| 4438 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4439 | connection_.OnCanWrite(); |
| 4440 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4441 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4442 | } |
| 4443 | |
| 4444 | TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) { |
| 4445 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4446 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 4447 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4448 | |
| 4449 | BlockOnNextWrite(); |
| 4450 | writer_->set_is_write_blocked_data_buffered(true); |
| 4451 | // Simulate the retransmission alarm firing. |
| 4452 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4453 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4454 | |
| 4455 | // Ack the sent packet before the callback returns, which happens in |
| 4456 | // rare circumstances with write blocked sockets. |
| 4457 | QuicAckFrame ack = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4458 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4459 | ProcessAckPacket(&ack); |
| 4460 | |
| 4461 | writer_->SetWritable(); |
| 4462 | connection_.OnCanWrite(); |
| 4463 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 4464 | EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, 3)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) { |
| 4468 | // Block the connection. |
| 4469 | BlockOnNextWrite(); |
| 4470 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 4471 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 4472 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4473 | |
| 4474 | // Set the send alarm. Fire the alarm and ensure it doesn't attempt to write. |
| 4475 | connection_.GetSendAlarm()->Set(clock_.ApproximateNow()); |
| 4476 | connection_.GetSendAlarm()->Fire(); |
| 4477 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4478 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 4479 | } |
| 4480 | |
| 4481 | TEST_P(QuicConnectionTest, NoSendAlarmAfterProcessPacketWhenWriteBlocked) { |
| 4482 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4483 | |
| 4484 | // Block the connection. |
| 4485 | BlockOnNextWrite(); |
| 4486 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 4487 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4488 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4489 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4490 | |
| 4491 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4492 | // Process packet number 1. Can not call ProcessPacket or ProcessDataPacket |
| 4493 | // here, because they will fire the alarm after QuicConnection::ProcessPacket |
| 4494 | // is returned. |
| 4495 | const uint64_t received_packet_num = 1; |
| 4496 | const bool has_stop_waiting = false; |
| 4497 | const EncryptionLevel level = ENCRYPTION_FORWARD_SECURE; |
| 4498 | std::unique_ptr<QuicPacket> packet( |
| 4499 | ConstructDataPacket(received_packet_num, has_stop_waiting, level)); |
| 4500 | char buffer[kMaxOutgoingPacketSize]; |
| 4501 | size_t encrypted_length = |
| 4502 | peer_framer_.EncryptPayload(level, QuicPacketNumber(received_packet_num), |
| 4503 | *packet, buffer, kMaxOutgoingPacketSize); |
| 4504 | connection_.ProcessUdpPacket( |
| 4505 | kSelfAddress, kPeerAddress, |
| 4506 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 4507 | |
| 4508 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4509 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4510 | } |
| 4511 | |
QUICHE team | 32d6279 | 2023-04-27 11:47:01 -0700 | [diff] [blame] | 4512 | TEST_P(QuicConnectionTest, SendAlarmNonZeroDelay) { |
| 4513 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4514 | // Set a 10 ms send alarm delay. The send alarm after processing the packet |
| 4515 | // should fire after waiting 10ms, not immediately. |
| 4516 | connection_.set_defer_send_in_response_to_packets(true); |
| 4517 | connection_.sent_packet_manager().SetDeferredSendAlarmDelay( |
| 4518 | QuicTime::Delta::FromMilliseconds(10)); |
| 4519 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4520 | |
| 4521 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4522 | // Process packet number 1. Can not call ProcessPacket or ProcessDataPacket |
| 4523 | // here, because they will fire the alarm after QuicConnection::ProcessPacket |
| 4524 | // is returned. |
| 4525 | const uint64_t received_packet_num = 1; |
| 4526 | const bool has_stop_waiting = false; |
| 4527 | const EncryptionLevel level = ENCRYPTION_FORWARD_SECURE; |
| 4528 | std::unique_ptr<QuicPacket> packet( |
| 4529 | ConstructDataPacket(received_packet_num, has_stop_waiting, level)); |
| 4530 | char buffer[kMaxOutgoingPacketSize]; |
| 4531 | size_t encrypted_length = |
| 4532 | peer_framer_.EncryptPayload(level, QuicPacketNumber(received_packet_num), |
| 4533 | *packet, buffer, kMaxOutgoingPacketSize); |
| 4534 | connection_.ProcessUdpPacket( |
| 4535 | kSelfAddress, kPeerAddress, |
| 4536 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 4537 | |
| 4538 | EXPECT_TRUE(connection_.GetSendAlarm()->IsSet()); |
| 4539 | // It was set to be 10 ms in the future, so it should at the least be greater |
| 4540 | // than now + 5 ms. |
| 4541 | EXPECT_TRUE(connection_.GetSendAlarm()->deadline() > |
| 4542 | clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5)); |
| 4543 | } |
| 4544 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4545 | TEST_P(QuicConnectionTest, AddToWriteBlockedListIfWriterBlockedWhenProcessing) { |
| 4546 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4547 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 4548 | |
| 4549 | // Simulate the case where a shared writer gets blocked by another connection. |
| 4550 | writer_->SetWriteBlocked(); |
| 4551 | |
| 4552 | // Process an ACK, make sure the connection calls visitor_->OnWriteBlocked(). |
| 4553 | QuicAckFrame ack1 = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4554 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4555 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 4556 | ProcessAckPacket(1, &ack1); |
| 4557 | } |
| 4558 | |
| 4559 | TEST_P(QuicConnectionTest, DoNotAddToWriteBlockedListAfterDisconnect) { |
| 4560 | writer_->SetBatchMode(true); |
| 4561 | EXPECT_TRUE(connection_.connected()); |
| 4562 | // Have to explicitly grab the OnConnectionClosed frame and check |
| 4563 | // its parameters because this is a silent connection close and the |
| 4564 | // frame is not also transmitted to the peer. |
| 4565 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 4566 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 4567 | |
| 4568 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(0); |
| 4569 | |
| 4570 | { |
| 4571 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 4572 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 4573 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 4574 | |
| 4575 | EXPECT_FALSE(connection_.connected()); |
| 4576 | writer_->SetWriteBlocked(); |
| 4577 | } |
| 4578 | EXPECT_EQ(1, connection_close_frame_count_); |
| 4579 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 4580 | IsError(QUIC_PEER_GOING_AWAY)); |
| 4581 | } |
| 4582 | |
| 4583 | TEST_P(QuicConnectionTest, AddToWriteBlockedListIfBlockedOnFlushPackets) { |
| 4584 | writer_->SetBatchMode(true); |
| 4585 | writer_->BlockOnNextFlush(); |
| 4586 | |
| 4587 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 4588 | { |
| 4589 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 4590 | // flusher's destructor will call connection_.FlushPackets, which should add |
| 4591 | // the connection to the write blocked list. |
| 4592 | } |
| 4593 | } |
| 4594 | |
| 4595 | TEST_P(QuicConnectionTest, NoLimitPacketsPerNack) { |
| 4596 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4597 | int offset = 0; |
| 4598 | // Send packets 1 to 15. |
| 4599 | for (int i = 0; i < 15; ++i) { |
| 4600 | SendStreamDataToPeer(1, "foo", offset, NO_FIN, nullptr); |
| 4601 | offset += 3; |
| 4602 | } |
| 4603 | |
| 4604 | // Ack 15, nack 1-14. |
| 4605 | |
| 4606 | QuicAckFrame nack = |
| 4607 | InitAckFrame({{QuicPacketNumber(15), QuicPacketNumber(16)}}); |
| 4608 | |
| 4609 | // 14 packets have been NACK'd and lost. |
| 4610 | LostPacketVector lost_packets; |
| 4611 | for (int i = 1; i < 15; ++i) { |
| 4612 | lost_packets.push_back( |
| 4613 | LostPacket(QuicPacketNumber(i), kMaxOutgoingPacketSize)); |
| 4614 | } |
| 4615 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 4616 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4617 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4618 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4619 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4620 | ProcessAckPacket(&nack); |
| 4621 | } |
| 4622 | |
| 4623 | // Test sending multiple acks from the connection to the session. |
| 4624 | TEST_P(QuicConnectionTest, MultipleAcks) { |
| 4625 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4626 | return; |
| 4627 | } |
| 4628 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4629 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4630 | ProcessDataPacket(1); |
| 4631 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 4632 | QuicPacketNumber last_packet; |
| 4633 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 4634 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4635 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); // Packet 2 |
| 4636 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 4637 | SendAckPacketToPeer(); // Packet 3 |
| 4638 | SendStreamDataToPeer(5, "foo", 0, NO_FIN, &last_packet); // Packet 4 |
| 4639 | EXPECT_EQ(QuicPacketNumber(4u), last_packet); |
| 4640 | SendStreamDataToPeer(1, "foo", 3, NO_FIN, &last_packet); // Packet 5 |
| 4641 | EXPECT_EQ(QuicPacketNumber(5u), last_packet); |
| 4642 | SendStreamDataToPeer(3, "foo", 3, NO_FIN, &last_packet); // Packet 6 |
| 4643 | EXPECT_EQ(QuicPacketNumber(6u), last_packet); |
| 4644 | |
| 4645 | // Client will ack packets 1, 2, [!3], 4, 5. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4646 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4647 | QuicAckFrame frame1 = ConstructAckFrame(5, 3); |
| 4648 | ProcessAckPacket(&frame1); |
| 4649 | |
| 4650 | // Now the client implicitly acks 3, and explicitly acks 6. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4651 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4652 | QuicAckFrame frame2 = InitAckFrame(6); |
| 4653 | ProcessAckPacket(&frame2); |
| 4654 | } |
| 4655 | |
| 4656 | TEST_P(QuicConnectionTest, DontLatchUnackedPacket) { |
| 4657 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4658 | return; |
| 4659 | } |
| 4660 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4661 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4662 | ProcessDataPacket(1); |
| 4663 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 4664 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); // Packet 1; |
| 4665 | // From now on, we send acks, so the send algorithm won't mark them pending. |
| 4666 | SendAckPacketToPeer(); // Packet 2 |
| 4667 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4668 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4669 | QuicAckFrame frame = InitAckFrame(1); |
| 4670 | ProcessAckPacket(&frame); |
| 4671 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4672 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4673 | frame = InitAckFrame(2); |
| 4674 | ProcessAckPacket(&frame); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4675 | |
| 4676 | // When we send an ack, we make sure our least-unacked makes sense. In this |
| 4677 | // case since we're not waiting on an ack for 2 and all packets are acked, we |
| 4678 | // set it to 3. |
| 4679 | SendAckPacketToPeer(); // Packet 3 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4680 | |
| 4681 | // Ack the ack, which updates the rtt and raises the least unacked. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4682 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4683 | frame = InitAckFrame(3); |
| 4684 | ProcessAckPacket(&frame); |
| 4685 | |
| 4686 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); // Packet 4 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4687 | SendAckPacketToPeer(); // Packet 5 |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4688 | |
| 4689 | // Send two data packets at the end, and ensure if the last one is acked, |
| 4690 | // the least unacked is raised above the ack packets. |
| 4691 | SendStreamDataToPeer(1, "bar", 6, NO_FIN, nullptr); // Packet 6 |
| 4692 | SendStreamDataToPeer(1, "bar", 9, NO_FIN, nullptr); // Packet 7 |
| 4693 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 4694 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4695 | frame = InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)}, |
| 4696 | {QuicPacketNumber(7), QuicPacketNumber(8)}}); |
| 4697 | ProcessAckPacket(&frame); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4698 | } |
| 4699 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4700 | TEST_P(QuicConnectionTest, SendHandshakeMessages) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4701 | // Attempt to send a handshake message and have the socket block. |
| 4702 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 4703 | BlockOnNextWrite(); |
| 4704 | connection_.SendCryptoDataWithString("foo", 0); |
| 4705 | // The packet should be serialized, but not queued. |
| 4706 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4707 | |
| 4708 | // Switch to the new encrypter. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4709 | connection_.SetEncrypter( |
| 4710 | ENCRYPTION_ZERO_RTT, |
| 4711 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4712 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4713 | |
| 4714 | // Now become writeable and flush the packets. |
| 4715 | writer_->SetWritable(); |
| 4716 | EXPECT_CALL(visitor_, OnCanWrite()); |
| 4717 | connection_.OnCanWrite(); |
| 4718 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4719 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4720 | // Verify that the handshake packet went out with Initial encryption. |
| 4721 | EXPECT_NE(0x02020202u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4722 | } |
| 4723 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4724 | TEST_P(QuicConnectionTest, DropRetransmitsForInitialPacketAfterForwardSecure) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4725 | connection_.SendCryptoStreamData(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4726 | // Simulate the retransmission alarm firing and the socket blocking. |
| 4727 | BlockOnNextWrite(); |
| 4728 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4729 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4730 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4731 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4732 | |
| 4733 | // Go forward secure. |
| 4734 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 4735 | std::make_unique<TaggingEncrypter>(0x02)); |
| 4736 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 4737 | notifier_.NeuterUnencryptedData(); |
| 4738 | connection_.NeuterUnencryptedPackets(); |
| 4739 | connection_.OnHandshakeComplete(); |
| 4740 | |
| 4741 | EXPECT_EQ(QuicTime::Zero(), connection_.GetRetransmissionAlarm()->deadline()); |
| 4742 | // Unblock the socket and ensure that no packets are sent. |
| 4743 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4744 | writer_->SetWritable(); |
| 4745 | connection_.OnCanWrite(); |
| 4746 | } |
| 4747 | |
| 4748 | TEST_P(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4749 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 4750 | |
| 4751 | connection_.SendCryptoDataWithString("foo", 0); |
| 4752 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4753 | connection_.SetEncrypter( |
| 4754 | ENCRYPTION_ZERO_RTT, |
| 4755 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4756 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4757 | if (!connection_.version().KnowsWhichDecrypterToUse()) { |
| 4758 | writer_->framer()->framer()->SetAlternativeDecrypter( |
| 4759 | ENCRYPTION_ZERO_RTT, |
| 4760 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT), false); |
| 4761 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4762 | |
| 4763 | SendStreamDataToPeer(2, "bar", 0, NO_FIN, nullptr); |
| 4764 | EXPECT_FALSE(notifier_.HasLostStreamData()); |
| 4765 | connection_.MarkZeroRttPacketsForRetransmission(0); |
| 4766 | EXPECT_TRUE(notifier_.HasLostStreamData()); |
| 4767 | } |
| 4768 | |
| 4769 | TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) { |
| 4770 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4771 | return; |
| 4772 | } |
| 4773 | // SetFromConfig is always called after construction from InitializeSession. |
| 4774 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4775 | QuicConfig config; |
| 4776 | connection_.SetFromConfig(config); |
| 4777 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4778 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4779 | peer_framer_.SetEncrypter( |
| 4780 | ENCRYPTION_ZERO_RTT, |
| 4781 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
| 4782 | if (!connection_.version().KnowsWhichDecrypterToUse()) { |
| 4783 | writer_->framer()->framer()->SetDecrypter( |
| 4784 | ENCRYPTION_ZERO_RTT, std::make_unique<TaggingDecrypter>()); |
| 4785 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4786 | |
| 4787 | // Process an encrypted packet which can not yet be decrypted which should |
| 4788 | // result in the packet being buffered. |
| 4789 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4790 | |
| 4791 | // Transition to the new encryption state and process another encrypted packet |
| 4792 | // which should result in the original packet being processed. |
| 4793 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4794 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 4795 | connection_.SetEncrypter( |
| 4796 | ENCRYPTION_ZERO_RTT, |
| 4797 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4798 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4799 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(2); |
| 4800 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4801 | |
| 4802 | // Finally, process a third packet and note that we do not reprocess the |
| 4803 | // buffered packet. |
| 4804 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4805 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4806 | } |
| 4807 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4808 | TEST_P(QuicConnectionTest, Buffer100NonDecryptablePacketsThenKeyChange) { |
| 4809 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4810 | return; |
| 4811 | } |
| 4812 | // SetFromConfig is always called after construction from InitializeSession. |
| 4813 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4814 | QuicConfig config; |
| 4815 | config.set_max_undecryptable_packets(100); |
| 4816 | connection_.SetFromConfig(config); |
| 4817 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4818 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4819 | peer_framer_.SetEncrypter( |
| 4820 | ENCRYPTION_ZERO_RTT, |
| 4821 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4822 | |
| 4823 | // Process an encrypted packet which can not yet be decrypted which should |
| 4824 | // result in the packet being buffered. |
| 4825 | for (uint64_t i = 1; i <= 100; ++i) { |
| 4826 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4827 | } |
| 4828 | |
| 4829 | // Transition to the new encryption state and process another encrypted packet |
| 4830 | // which should result in the original packets being processed. |
| 4831 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 4832 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4833 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4834 | EXPECT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4835 | connection_.SetEncrypter( |
| 4836 | ENCRYPTION_ZERO_RTT, |
| 4837 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4838 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4839 | |
| 4840 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(100); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 4841 | if (!connection_.version().KnowsWhichDecrypterToUse()) { |
| 4842 | writer_->framer()->framer()->SetDecrypter( |
| 4843 | ENCRYPTION_ZERO_RTT, std::make_unique<TaggingDecrypter>()); |
| 4844 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4845 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 4846 | |
| 4847 | // Finally, process a third packet and note that we do not reprocess the |
| 4848 | // buffered packet. |
| 4849 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4850 | ProcessDataPacketAtLevel(102, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4851 | } |
| 4852 | |
| 4853 | TEST_P(QuicConnectionTest, SetRTOAfterWritingToSocket) { |
| 4854 | BlockOnNextWrite(); |
| 4855 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4856 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4857 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4858 | |
| 4859 | // Test that RTO is started once we write to the socket. |
| 4860 | writer_->SetWritable(); |
| 4861 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4862 | connection_.OnCanWrite(); |
| 4863 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4864 | } |
| 4865 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4866 | TEST_P(QuicConnectionTest, TestQueued) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 4867 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4868 | BlockOnNextWrite(); |
| 4869 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4870 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4871 | |
| 4872 | // Unblock the writes and actually send. |
| 4873 | writer_->SetWritable(); |
| 4874 | connection_.OnCanWrite(); |
| 4875 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4876 | } |
| 4877 | |
| 4878 | TEST_P(QuicConnectionTest, InitialTimeout) { |
| 4879 | EXPECT_TRUE(connection_.connected()); |
| 4880 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4881 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4882 | |
| 4883 | // SetFromConfig sets the initial timeouts before negotiation. |
| 4884 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4885 | QuicConfig config; |
| 4886 | connection_.SetFromConfig(config); |
| 4887 | // Subtract a second from the idle timeout on the client side. |
| 4888 | QuicTime default_timeout = |
| 4889 | clock_.ApproximateNow() + |
| 4890 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4891 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 4892 | |
| 4893 | EXPECT_CALL(visitor_, |
| 4894 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 4895 | // Simulate the timeout alarm firing. |
| 4896 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1)); |
| 4897 | connection_.GetTimeoutAlarm()->Fire(); |
| 4898 | |
| 4899 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4900 | EXPECT_FALSE(connection_.connected()); |
| 4901 | |
| 4902 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 4903 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4904 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4905 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4906 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 4907 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 4908 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 4909 | } |
| 4910 | |
| 4911 | TEST_P(QuicConnectionTest, IdleTimeoutAfterFirstSentPacket) { |
| 4912 | EXPECT_TRUE(connection_.connected()); |
| 4913 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4914 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4915 | |
| 4916 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4917 | QuicConfig config; |
| 4918 | connection_.SetFromConfig(config); |
| 4919 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4920 | QuicTime initial_ddl = |
| 4921 | clock_.ApproximateNow() + |
| 4922 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4923 | EXPECT_EQ(initial_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4924 | EXPECT_TRUE(connection_.connected()); |
| 4925 | |
| 4926 | // Advance the time and send the first packet to the peer. |
| 4927 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 4928 | QuicPacketNumber last_packet; |
| 4929 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4930 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4931 | // This will be the updated deadline for the connection to idle time out. |
| 4932 | QuicTime new_ddl = clock_.ApproximateNow() + |
| 4933 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4934 | |
| 4935 | // Simulate the timeout alarm firing, the connection should not be closed as |
| 4936 | // a new packet has been sent. |
| 4937 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 4938 | QuicTime::Delta delay = initial_ddl - clock_.ApproximateNow(); |
| 4939 | clock_.AdvanceTime(delay); |
| 4940 | // Verify the timeout alarm deadline is updated. |
| 4941 | EXPECT_TRUE(connection_.connected()); |
| 4942 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4943 | EXPECT_EQ(new_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4944 | |
| 4945 | // Simulate the timeout alarm firing again, the connection now should be |
| 4946 | // closed. |
| 4947 | EXPECT_CALL(visitor_, |
| 4948 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 4949 | clock_.AdvanceTime(new_ddl - clock_.ApproximateNow()); |
| 4950 | connection_.GetTimeoutAlarm()->Fire(); |
| 4951 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4952 | EXPECT_FALSE(connection_.connected()); |
| 4953 | |
| 4954 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 4955 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4956 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4957 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4958 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 4959 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 4960 | } |
| 4961 | |
| 4962 | TEST_P(QuicConnectionTest, IdleTimeoutAfterSendTwoPackets) { |
| 4963 | EXPECT_TRUE(connection_.connected()); |
| 4964 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4965 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4966 | |
| 4967 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4968 | QuicConfig config; |
| 4969 | connection_.SetFromConfig(config); |
| 4970 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4971 | QuicTime initial_ddl = |
| 4972 | clock_.ApproximateNow() + |
| 4973 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4974 | EXPECT_EQ(initial_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4975 | EXPECT_TRUE(connection_.connected()); |
| 4976 | |
| 4977 | // Immediately send the first packet, this is a rare case but test code will |
| 4978 | // hit this issue often as MockClock used for tests doesn't move with code |
| 4979 | // execution until manually adjusted. |
| 4980 | QuicPacketNumber last_packet; |
| 4981 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4982 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4983 | |
| 4984 | // Advance the time and send the second packet to the peer. |
| 4985 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 4986 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4987 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 4988 | |
| 4989 | // Simulate the timeout alarm firing, the connection will be closed. |
| 4990 | EXPECT_CALL(visitor_, |
| 4991 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 4992 | clock_.AdvanceTime(initial_ddl - clock_.ApproximateNow()); |
| 4993 | connection_.GetTimeoutAlarm()->Fire(); |
| 4994 | |
| 4995 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4996 | EXPECT_FALSE(connection_.connected()); |
| 4997 | |
| 4998 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 4999 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5000 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 5001 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 5002 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5003 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 5004 | } |
| 5005 | |
| 5006 | TEST_P(QuicConnectionTest, HandshakeTimeout) { |
| 5007 | // Use a shorter handshake timeout than idle timeout for this test. |
| 5008 | const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(5); |
| 5009 | connection_.SetNetworkTimeouts(timeout, timeout); |
| 5010 | EXPECT_TRUE(connection_.connected()); |
| 5011 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 5012 | |
| 5013 | QuicTime handshake_timeout = |
| 5014 | clock_.ApproximateNow() + timeout - QuicTime::Delta::FromSeconds(1); |
| 5015 | EXPECT_EQ(handshake_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5016 | EXPECT_TRUE(connection_.connected()); |
| 5017 | |
| 5018 | // Send and ack new data 3 seconds later to lengthen the idle timeout. |
| 5019 | SendStreamDataToPeer( |
| 5020 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 5021 | "GET /", 0, FIN, nullptr); |
| 5022 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(3)); |
| 5023 | QuicAckFrame frame = InitAckFrame(1); |
| 5024 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5025 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5026 | ProcessAckPacket(&frame); |
| 5027 | |
| 5028 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5029 | EXPECT_TRUE(connection_.connected()); |
| 5030 | |
| 5031 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromSeconds(2)); |
| 5032 | |
| 5033 | EXPECT_CALL(visitor_, |
| 5034 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 5035 | // Simulate the timeout alarm firing. |
| 5036 | connection_.GetTimeoutAlarm()->Fire(); |
| 5037 | |
| 5038 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5039 | EXPECT_FALSE(connection_.connected()); |
| 5040 | |
| 5041 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 5042 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5043 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 5044 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 5045 | TestConnectionCloseQuicErrorCode(QUIC_HANDSHAKE_TIMEOUT); |
| 5046 | } |
| 5047 | |
| 5048 | TEST_P(QuicConnectionTest, PingAfterSend) { |
| 5049 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 5050 | return; |
| 5051 | } |
| 5052 | EXPECT_TRUE(connection_.connected()); |
| 5053 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5054 | .WillRepeatedly(Return(true)); |
| 5055 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5056 | |
| 5057 | // Advance to 5ms, and send a packet to the peer, which will set |
| 5058 | // the ping alarm. |
| 5059 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5060 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 5061 | SendStreamDataToPeer( |
| 5062 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 5063 | "GET /", 0, FIN, nullptr); |
| 5064 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 5065 | EXPECT_EQ(QuicTime::Delta::FromSeconds(15), |
| 5066 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 5067 | |
| 5068 | // Now recevie an ACK of the previous packet, which will move the |
| 5069 | // ping alarm forward. |
| 5070 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5071 | QuicAckFrame frame = InitAckFrame(1); |
| 5072 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5073 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5074 | ProcessAckPacket(&frame); |
| 5075 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 5076 | // The ping timer is set slightly less than 15 seconds in the future, because |
| 5077 | // of the 1s ping timer alarm granularity. |
| 5078 | EXPECT_EQ( |
| 5079 | QuicTime::Delta::FromSeconds(15) - QuicTime::Delta::FromMilliseconds(5), |
| 5080 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 5081 | |
| 5082 | writer_->Reset(); |
| 5083 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(15)); |
| 5084 | connection_.GetPingAlarm()->Fire(); |
| 5085 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 5086 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 5087 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 5088 | writer_->Reset(); |
| 5089 | |
| 5090 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5091 | .WillRepeatedly(Return(false)); |
| 5092 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5093 | SendAckPacketToPeer(); |
| 5094 | |
| 5095 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5096 | } |
| 5097 | |
| 5098 | TEST_P(QuicConnectionTest, ReducedPingTimeout) { |
| 5099 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 5100 | return; |
| 5101 | } |
| 5102 | EXPECT_TRUE(connection_.connected()); |
| 5103 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5104 | .WillRepeatedly(Return(true)); |
| 5105 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5106 | |
| 5107 | // Use a reduced ping timeout for this connection. |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 5108 | connection_.set_keep_alive_ping_timeout(QuicTime::Delta::FromSeconds(10)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5109 | |
| 5110 | // Advance to 5ms, and send a packet to the peer, which will set |
| 5111 | // the ping alarm. |
| 5112 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5113 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 5114 | SendStreamDataToPeer( |
| 5115 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 5116 | "GET /", 0, FIN, nullptr); |
| 5117 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 5118 | EXPECT_EQ(QuicTime::Delta::FromSeconds(10), |
| 5119 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 5120 | |
| 5121 | // Now recevie an ACK of the previous packet, which will move the |
| 5122 | // ping alarm forward. |
| 5123 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5124 | QuicAckFrame frame = InitAckFrame(1); |
| 5125 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5126 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5127 | ProcessAckPacket(&frame); |
| 5128 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 5129 | // The ping timer is set slightly less than 10 seconds in the future, because |
| 5130 | // of the 1s ping timer alarm granularity. |
| 5131 | EXPECT_EQ( |
| 5132 | QuicTime::Delta::FromSeconds(10) - QuicTime::Delta::FromMilliseconds(5), |
| 5133 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 5134 | |
| 5135 | writer_->Reset(); |
| 5136 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); |
| 5137 | connection_.GetPingAlarm()->Fire(); |
| 5138 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 5139 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 5140 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 5141 | writer_->Reset(); |
| 5142 | |
| 5143 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5144 | .WillRepeatedly(Return(false)); |
| 5145 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5146 | SendAckPacketToPeer(); |
| 5147 | |
| 5148 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5149 | } |
| 5150 | |
| 5151 | // Tests whether sending an MTU discovery packet to peer successfully causes the |
| 5152 | // maximum packet size to increase. |
| 5153 | TEST_P(QuicConnectionTest, SendMtuDiscoveryPacket) { |
| 5154 | MtuDiscoveryTestInit(); |
| 5155 | |
| 5156 | // Send an MTU probe. |
| 5157 | const size_t new_mtu = kDefaultMaxPacketSize + 100; |
| 5158 | QuicByteCount mtu_probe_size; |
| 5159 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5160 | .WillOnce(SaveArg<3>(&mtu_probe_size)); |
| 5161 | connection_.SendMtuDiscoveryPacket(new_mtu); |
| 5162 | EXPECT_EQ(new_mtu, mtu_probe_size); |
| 5163 | EXPECT_EQ(QuicPacketNumber(1u), creator_->packet_number()); |
| 5164 | |
| 5165 | // Send more than MTU worth of data. No acknowledgement was received so far, |
| 5166 | // so the MTU should be at its old value. |
| 5167 | const std::string data(kDefaultMaxPacketSize + 1, '.'); |
| 5168 | QuicByteCount size_before_mtu_change; |
| 5169 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5170 | .Times(2) |
| 5171 | .WillOnce(SaveArg<3>(&size_before_mtu_change)) |
| 5172 | .WillOnce(Return()); |
| 5173 | connection_.SendStreamDataWithString(3, data, 0, FIN); |
| 5174 | EXPECT_EQ(QuicPacketNumber(3u), creator_->packet_number()); |
| 5175 | EXPECT_EQ(kDefaultMaxPacketSize, size_before_mtu_change); |
| 5176 | |
| 5177 | // Acknowledge all packets so far. |
| 5178 | QuicAckFrame probe_ack = InitAckFrame(3); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5179 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5180 | ProcessAckPacket(&probe_ack); |
| 5181 | EXPECT_EQ(new_mtu, connection_.max_packet_length()); |
| 5182 | |
| 5183 | // Send the same data again. Check that it fits into a single packet now. |
| 5184 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 5185 | connection_.SendStreamDataWithString(3, data, 0, FIN); |
| 5186 | EXPECT_EQ(QuicPacketNumber(4u), creator_->packet_number()); |
| 5187 | } |
| 5188 | |
| 5189 | // Verifies that when a MTU probe packet is sent and buffered in a batch writer, |
| 5190 | // the writer is flushed immediately. |
| 5191 | TEST_P(QuicConnectionTest, BatchWriterFlushedAfterMtuDiscoveryPacket) { |
| 5192 | writer_->SetBatchMode(true); |
| 5193 | MtuDiscoveryTestInit(); |
| 5194 | |
| 5195 | // Send an MTU probe. |
| 5196 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
| 5197 | QuicByteCount mtu_probe_size; |
| 5198 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5199 | .WillOnce(SaveArg<3>(&mtu_probe_size)); |
| 5200 | const uint32_t prior_flush_attempts = writer_->flush_attempts(); |
| 5201 | connection_.SendMtuDiscoveryPacket(target_mtu); |
| 5202 | EXPECT_EQ(target_mtu, mtu_probe_size); |
| 5203 | EXPECT_EQ(writer_->flush_attempts(), prior_flush_attempts + 1); |
| 5204 | } |
| 5205 | |
| 5206 | // Tests whether MTU discovery does not happen when it is not explicitly enabled |
| 5207 | // by the connection options. |
| 5208 | TEST_P(QuicConnectionTest, MtuDiscoveryDisabled) { |
| 5209 | MtuDiscoveryTestInit(); |
| 5210 | |
| 5211 | const QuicPacketCount packets_between_probes_base = 10; |
| 5212 | set_packets_between_probes_base(packets_between_probes_base); |
| 5213 | |
| 5214 | const QuicPacketCount number_of_packets = packets_between_probes_base * 2; |
| 5215 | for (QuicPacketCount i = 0; i < number_of_packets; i++) { |
| 5216 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5217 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5218 | EXPECT_EQ(0u, connection_.mtu_probe_count()); |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | // Tests whether MTU discovery works when all probes are acknowledged on the |
| 5223 | // first try. |
| 5224 | TEST_P(QuicConnectionTest, MtuDiscoveryEnabled) { |
| 5225 | MtuDiscoveryTestInit(); |
| 5226 | |
| 5227 | const QuicPacketCount packets_between_probes_base = 5; |
| 5228 | set_packets_between_probes_base(packets_between_probes_base); |
| 5229 | |
| 5230 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5231 | |
| 5232 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5233 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5234 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5235 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5236 | } |
| 5237 | |
| 5238 | // Trigger the probe. |
| 5239 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5240 | nullptr); |
| 5241 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5242 | QuicByteCount probe_size; |
| 5243 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5244 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5245 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5246 | |
| 5247 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5248 | kMtuDiscoveryTargetPacketSizeHigh)); |
| 5249 | |
| 5250 | const QuicPacketNumber probe_packet_number = |
| 5251 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5252 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5253 | |
| 5254 | // Acknowledge all packets sent so far. |
| 5255 | QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5256 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5257 | .Times(AnyNumber()); |
| 5258 | ProcessAckPacket(&probe_ack); |
| 5259 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5260 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5261 | |
| 5262 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5263 | |
| 5264 | QuicStreamOffset stream_offset = packets_between_probes_base; |
| 5265 | QuicByteCount last_probe_size = 0; |
| 5266 | for (size_t num_probes = 1; num_probes < kMtuDiscoveryAttempts; |
| 5267 | ++num_probes) { |
| 5268 | // Send just enough packets without triggering the next probe. |
| 5269 | for (QuicPacketCount i = 0; |
| 5270 | i < (packets_between_probes_base << num_probes) - 1; ++i) { |
| 5271 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5272 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5273 | } |
| 5274 | |
| 5275 | // Trigger the next probe. |
| 5276 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5277 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5278 | QuicByteCount new_probe_size; |
| 5279 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5280 | .WillOnce(SaveArg<3>(&new_probe_size)); |
| 5281 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5282 | EXPECT_THAT(new_probe_size, |
| 5283 | InRange(probe_size, kMtuDiscoveryTargetPacketSizeHigh)); |
| 5284 | EXPECT_EQ(num_probes + 1, connection_.mtu_probe_count()); |
| 5285 | |
| 5286 | // Acknowledge all packets sent so far. |
| 5287 | QuicAckFrame probe_ack = InitAckFrame(creator_->packet_number()); |
| 5288 | ProcessAckPacket(&probe_ack); |
| 5289 | EXPECT_EQ(new_probe_size, connection_.max_packet_length()); |
| 5290 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5291 | |
| 5292 | last_probe_size = probe_size; |
| 5293 | probe_size = new_probe_size; |
| 5294 | } |
| 5295 | |
| 5296 | // The last probe size should be equal to the target. |
| 5297 | EXPECT_EQ(probe_size, kMtuDiscoveryTargetPacketSizeHigh); |
| 5298 | |
| 5299 | writer_->SetShouldWriteFail(); |
| 5300 | |
| 5301 | // Ignore PACKET_WRITE_ERROR once. |
| 5302 | SendStreamDataToPeer(3, "(", stream_offset++, NO_FIN, nullptr); |
| 5303 | EXPECT_EQ(last_probe_size, connection_.max_packet_length()); |
| 5304 | EXPECT_TRUE(connection_.connected()); |
| 5305 | |
| 5306 | // Close connection on another PACKET_WRITE_ERROR. |
| 5307 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)) |
| 5308 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5309 | SendStreamDataToPeer(3, ")", stream_offset++, NO_FIN, nullptr); |
| 5310 | EXPECT_EQ(last_probe_size, connection_.max_packet_length()); |
| 5311 | EXPECT_FALSE(connection_.connected()); |
| 5312 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5313 | IsError(QUIC_PACKET_WRITE_ERROR)); |
| 5314 | } |
| 5315 | |
| 5316 | // After a successful MTU probe, one and only one write error should be ignored |
| 5317 | // if it happened in QuicConnection::FlushPacket. |
| 5318 | TEST_P(QuicConnectionTest, |
| 5319 | MtuDiscoveryIgnoreOneWriteErrorInFlushAfterSuccessfulProbes) { |
| 5320 | MtuDiscoveryTestInit(); |
| 5321 | writer_->SetBatchMode(true); |
| 5322 | |
| 5323 | const QuicPacketCount packets_between_probes_base = 5; |
| 5324 | set_packets_between_probes_base(packets_between_probes_base); |
| 5325 | |
| 5326 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5327 | |
| 5328 | const QuicByteCount original_max_packet_length = |
| 5329 | connection_.max_packet_length(); |
| 5330 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5331 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5332 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5333 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5334 | } |
| 5335 | |
| 5336 | // Trigger the probe. |
| 5337 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5338 | nullptr); |
| 5339 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5340 | QuicByteCount probe_size; |
| 5341 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5342 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5343 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5344 | |
| 5345 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5346 | kMtuDiscoveryTargetPacketSizeHigh)); |
| 5347 | |
| 5348 | const QuicPacketNumber probe_packet_number = |
| 5349 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5350 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5351 | |
| 5352 | // Acknowledge all packets sent so far. |
| 5353 | QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5354 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5355 | .Times(AnyNumber()); |
| 5356 | ProcessAckPacket(&probe_ack); |
| 5357 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5358 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5359 | |
| 5360 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5361 | |
| 5362 | writer_->SetShouldWriteFail(); |
| 5363 | |
| 5364 | // Ignore PACKET_WRITE_ERROR once. |
| 5365 | { |
| 5366 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 5367 | // flusher's destructor will call connection_.FlushPackets, which should |
| 5368 | // get a WRITE_STATUS_ERROR from the writer and ignore it. |
| 5369 | } |
| 5370 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5371 | EXPECT_TRUE(connection_.connected()); |
| 5372 | |
| 5373 | // Close connection on another PACKET_WRITE_ERROR. |
| 5374 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)) |
| 5375 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5376 | { |
| 5377 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 5378 | // flusher's destructor will call connection_.FlushPackets, which should |
| 5379 | // get a WRITE_STATUS_ERROR from the writer and ignore it. |
| 5380 | } |
| 5381 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5382 | EXPECT_FALSE(connection_.connected()); |
| 5383 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5384 | IsError(QUIC_PACKET_WRITE_ERROR)); |
| 5385 | } |
| 5386 | |
| 5387 | // Simulate the case where the first attempt to send a probe is write blocked, |
| 5388 | // and after unblock, the second attempt returns a MSG_TOO_BIG error. |
| 5389 | TEST_P(QuicConnectionTest, MtuDiscoveryWriteBlocked) { |
| 5390 | MtuDiscoveryTestInit(); |
| 5391 | |
| 5392 | const QuicPacketCount packets_between_probes_base = 5; |
| 5393 | set_packets_between_probes_base(packets_between_probes_base); |
| 5394 | |
| 5395 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5396 | |
| 5397 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5398 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5399 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5400 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5401 | } |
| 5402 | |
| 5403 | QuicByteCount original_max_packet_length = connection_.max_packet_length(); |
| 5404 | |
| 5405 | // Trigger the probe. |
| 5406 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5407 | nullptr); |
| 5408 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5409 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 5410 | BlockOnNextWrite(); |
| 5411 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 5412 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5413 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5414 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 5415 | ASSERT_TRUE(connection_.connected()); |
| 5416 | |
| 5417 | writer_->SetWritable(); |
| 5418 | SimulateNextPacketTooLarge(); |
| 5419 | connection_.OnCanWrite(); |
| 5420 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 5421 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5422 | EXPECT_TRUE(connection_.connected()); |
| 5423 | } |
| 5424 | |
| 5425 | // Tests whether MTU discovery works correctly when the probes never get |
| 5426 | // acknowledged. |
| 5427 | TEST_P(QuicConnectionTest, MtuDiscoveryFailed) { |
| 5428 | MtuDiscoveryTestInit(); |
| 5429 | |
| 5430 | // Lower the number of probes between packets in order to make the test go |
| 5431 | // much faster. |
| 5432 | const QuicPacketCount packets_between_probes_base = 5; |
| 5433 | set_packets_between_probes_base(packets_between_probes_base); |
| 5434 | |
| 5435 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5436 | |
| 5437 | const QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(100); |
| 5438 | |
| 5439 | EXPECT_EQ(packets_between_probes_base, |
| 5440 | QuicConnectionPeer::GetPacketsBetweenMtuProbes(&connection_)); |
| 5441 | |
| 5442 | // This tests sends more packets than strictly necessary to make sure that if |
| 5443 | // the connection was to send more discovery packets than needed, those would |
| 5444 | // get caught as well. |
| 5445 | const QuicPacketCount number_of_packets = |
| 5446 | packets_between_probes_base * (1 << (kMtuDiscoveryAttempts + 1)); |
| 5447 | std::vector<QuicPacketNumber> mtu_discovery_packets; |
| 5448 | // Called on many acks. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5449 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5450 | .Times(AnyNumber()); |
| 5451 | for (QuicPacketCount i = 0; i < number_of_packets; i++) { |
| 5452 | SendStreamDataToPeer(3, "!", i, NO_FIN, nullptr); |
| 5453 | clock_.AdvanceTime(rtt); |
| 5454 | |
| 5455 | // Receive an ACK, which marks all data packets as received, and all MTU |
| 5456 | // discovery packets as missing. |
| 5457 | |
| 5458 | QuicAckFrame ack; |
| 5459 | |
| 5460 | if (!mtu_discovery_packets.empty()) { |
| 5461 | QuicPacketNumber min_packet = *min_element(mtu_discovery_packets.begin(), |
| 5462 | mtu_discovery_packets.end()); |
| 5463 | QuicPacketNumber max_packet = *max_element(mtu_discovery_packets.begin(), |
| 5464 | mtu_discovery_packets.end()); |
| 5465 | ack.packets.AddRange(QuicPacketNumber(1), min_packet); |
| 5466 | ack.packets.AddRange(QuicPacketNumber(max_packet + 1), |
| 5467 | creator_->packet_number() + 1); |
| 5468 | ack.largest_acked = creator_->packet_number(); |
| 5469 | |
| 5470 | } else { |
| 5471 | ack.packets.AddRange(QuicPacketNumber(1), creator_->packet_number() + 1); |
| 5472 | ack.largest_acked = creator_->packet_number(); |
| 5473 | } |
| 5474 | |
| 5475 | ProcessAckPacket(&ack); |
| 5476 | |
| 5477 | // Trigger MTU probe if it would be scheduled now. |
| 5478 | if (!connection_.GetMtuDiscoveryAlarm()->IsSet()) { |
| 5479 | continue; |
| 5480 | } |
| 5481 | |
| 5482 | // Fire the alarm. The alarm should cause a packet to be sent. |
| 5483 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 5484 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5485 | // Record the packet number of the MTU discovery packet in order to |
| 5486 | // mark it as NACK'd. |
| 5487 | mtu_discovery_packets.push_back(creator_->packet_number()); |
| 5488 | } |
| 5489 | |
| 5490 | // Ensure the number of packets between probes grows exponentially by checking |
| 5491 | // it against the closed-form expression for the packet number. |
| 5492 | ASSERT_EQ(kMtuDiscoveryAttempts, mtu_discovery_packets.size()); |
| 5493 | for (uint64_t i = 0; i < kMtuDiscoveryAttempts; i++) { |
| 5494 | // 2^0 + 2^1 + 2^2 + ... + 2^n = 2^(n + 1) - 1 |
| 5495 | const QuicPacketCount packets_between_probes = |
| 5496 | packets_between_probes_base * ((1 << (i + 1)) - 1); |
| 5497 | EXPECT_EQ(QuicPacketNumber(packets_between_probes + (i + 1)), |
| 5498 | mtu_discovery_packets[i]); |
| 5499 | } |
| 5500 | |
| 5501 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5502 | EXPECT_EQ(kDefaultMaxPacketSize, connection_.max_packet_length()); |
| 5503 | EXPECT_EQ(kMtuDiscoveryAttempts, connection_.mtu_probe_count()); |
| 5504 | } |
| 5505 | |
| 5506 | // Probe 3 times, the first one succeeds, then fails, then succeeds again. |
| 5507 | TEST_P(QuicConnectionTest, MtuDiscoverySecondProbeFailed) { |
| 5508 | MtuDiscoveryTestInit(); |
| 5509 | |
| 5510 | const QuicPacketCount packets_between_probes_base = 5; |
| 5511 | set_packets_between_probes_base(packets_between_probes_base); |
| 5512 | |
| 5513 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5514 | |
| 5515 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5516 | QuicStreamOffset stream_offset = 0; |
| 5517 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5518 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5519 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5520 | } |
| 5521 | |
| 5522 | // Trigger the probe. |
| 5523 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5524 | nullptr); |
| 5525 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5526 | QuicByteCount probe_size; |
| 5527 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5528 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5529 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5530 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5531 | kMtuDiscoveryTargetPacketSizeHigh)); |
| 5532 | |
| 5533 | const QuicPacketNumber probe_packet_number = |
| 5534 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5535 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5536 | |
| 5537 | // Acknowledge all packets sent so far. |
| 5538 | QuicAckFrame first_ack = InitAckFrame(probe_packet_number); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5539 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5540 | .Times(AnyNumber()); |
| 5541 | ProcessAckPacket(&first_ack); |
| 5542 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5543 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5544 | |
| 5545 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5546 | |
| 5547 | // Send just enough packets without triggering the second probe. |
| 5548 | for (QuicPacketCount i = 0; i < (packets_between_probes_base << 1) - 1; ++i) { |
| 5549 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5550 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5551 | } |
| 5552 | |
| 5553 | // Trigger the second probe. |
| 5554 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5555 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5556 | QuicByteCount second_probe_size; |
| 5557 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5558 | .WillOnce(SaveArg<3>(&second_probe_size)); |
| 5559 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5560 | EXPECT_THAT(second_probe_size, |
| 5561 | InRange(probe_size, kMtuDiscoveryTargetPacketSizeHigh)); |
| 5562 | EXPECT_EQ(2u, connection_.mtu_probe_count()); |
| 5563 | |
| 5564 | // Acknowledge all packets sent so far, except the second probe. |
| 5565 | QuicPacketNumber second_probe_packet_number = creator_->packet_number(); |
| 5566 | QuicAckFrame second_ack = InitAckFrame(second_probe_packet_number - 1); |
| 5567 | ProcessAckPacket(&first_ack); |
| 5568 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5569 | |
| 5570 | // Send just enough packets without triggering the third probe. |
| 5571 | for (QuicPacketCount i = 0; i < (packets_between_probes_base << 2) - 1; ++i) { |
| 5572 | SendStreamDataToPeer(3, "@", stream_offset++, NO_FIN, nullptr); |
| 5573 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5574 | } |
| 5575 | |
| 5576 | // Trigger the third probe. |
| 5577 | SendStreamDataToPeer(3, "#", stream_offset++, NO_FIN, nullptr); |
| 5578 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5579 | QuicByteCount third_probe_size; |
| 5580 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5581 | .WillOnce(SaveArg<3>(&third_probe_size)); |
| 5582 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5583 | EXPECT_THAT(third_probe_size, InRange(probe_size, second_probe_size)); |
| 5584 | EXPECT_EQ(3u, connection_.mtu_probe_count()); |
| 5585 | |
| 5586 | // Acknowledge all packets sent so far, except the second probe. |
| 5587 | QuicAckFrame third_ack = |
| 5588 | ConstructAckFrame(creator_->packet_number(), second_probe_packet_number); |
| 5589 | ProcessAckPacket(&third_ack); |
| 5590 | EXPECT_EQ(third_probe_size, connection_.max_packet_length()); |
| 5591 | |
| 5592 | SendStreamDataToPeer(3, "$", stream_offset++, NO_FIN, nullptr); |
| 5593 | EXPECT_TRUE(connection_.PathMtuReductionDetectionInProgress()); |
| 5594 | |
| 5595 | if (connection_.PathDegradingDetectionInProgress() && |
| 5596 | QuicConnectionPeer::GetPathDegradingDeadline(&connection_) < |
| 5597 | QuicConnectionPeer::GetPathMtuReductionDetectionDeadline( |
| 5598 | &connection_)) { |
| 5599 | // Fire path degrading alarm first. |
| 5600 | connection_.PathDegradingTimeout(); |
| 5601 | } |
| 5602 | |
| 5603 | // Verify the max packet size has not reduced. |
| 5604 | EXPECT_EQ(third_probe_size, connection_.max_packet_length()); |
| 5605 | |
| 5606 | // Fire alarm to get path mtu reduction callback called. |
| 5607 | EXPECT_TRUE(connection_.PathMtuReductionDetectionInProgress()); |
| 5608 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
| 5609 | |
| 5610 | // Verify the max packet size has reduced to the previous value. |
| 5611 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5612 | } |
| 5613 | |
| 5614 | // Tests whether MTU discovery works when the writer has a limit on how large a |
| 5615 | // packet can be. |
| 5616 | TEST_P(QuicConnectionTest, MtuDiscoveryWriterLimited) { |
| 5617 | MtuDiscoveryTestInit(); |
| 5618 | |
| 5619 | const QuicByteCount mtu_limit = kMtuDiscoveryTargetPacketSizeHigh - 1; |
| 5620 | writer_->set_max_packet_size(mtu_limit); |
| 5621 | |
| 5622 | const QuicPacketCount packets_between_probes_base = 5; |
| 5623 | set_packets_between_probes_base(packets_between_probes_base); |
| 5624 | |
| 5625 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5626 | |
| 5627 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5628 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5629 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5630 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5631 | } |
| 5632 | |
| 5633 | // Trigger the probe. |
| 5634 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5635 | nullptr); |
| 5636 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5637 | QuicByteCount probe_size; |
| 5638 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5639 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5640 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5641 | |
| 5642 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), mtu_limit)); |
| 5643 | |
| 5644 | const QuicPacketNumber probe_sequence_number = |
| 5645 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5646 | ASSERT_EQ(probe_sequence_number, creator_->packet_number()); |
| 5647 | |
| 5648 | // Acknowledge all packets sent so far. |
| 5649 | QuicAckFrame probe_ack = InitAckFrame(probe_sequence_number); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5650 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5651 | .Times(AnyNumber()); |
| 5652 | ProcessAckPacket(&probe_ack); |
| 5653 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5654 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5655 | |
| 5656 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5657 | |
| 5658 | QuicStreamOffset stream_offset = packets_between_probes_base; |
| 5659 | for (size_t num_probes = 1; num_probes < kMtuDiscoveryAttempts; |
| 5660 | ++num_probes) { |
| 5661 | // Send just enough packets without triggering the next probe. |
| 5662 | for (QuicPacketCount i = 0; |
| 5663 | i < (packets_between_probes_base << num_probes) - 1; ++i) { |
| 5664 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5665 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5666 | } |
| 5667 | |
| 5668 | // Trigger the next probe. |
| 5669 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5670 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5671 | QuicByteCount new_probe_size; |
| 5672 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5673 | .WillOnce(SaveArg<3>(&new_probe_size)); |
| 5674 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5675 | EXPECT_THAT(new_probe_size, InRange(probe_size, mtu_limit)); |
| 5676 | EXPECT_EQ(num_probes + 1, connection_.mtu_probe_count()); |
| 5677 | |
| 5678 | // Acknowledge all packets sent so far. |
| 5679 | QuicAckFrame probe_ack = InitAckFrame(creator_->packet_number()); |
| 5680 | ProcessAckPacket(&probe_ack); |
| 5681 | EXPECT_EQ(new_probe_size, connection_.max_packet_length()); |
| 5682 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5683 | |
| 5684 | probe_size = new_probe_size; |
| 5685 | } |
| 5686 | |
| 5687 | // The last probe size should be equal to the target. |
| 5688 | EXPECT_EQ(probe_size, mtu_limit); |
| 5689 | } |
| 5690 | |
| 5691 | // Tests whether MTU discovery works when the writer returns an error despite |
| 5692 | // advertising higher packet length. |
| 5693 | TEST_P(QuicConnectionTest, MtuDiscoveryWriterFailed) { |
| 5694 | MtuDiscoveryTestInit(); |
| 5695 | |
| 5696 | const QuicByteCount mtu_limit = kMtuDiscoveryTargetPacketSizeHigh - 1; |
| 5697 | const QuicByteCount initial_mtu = connection_.max_packet_length(); |
| 5698 | EXPECT_LT(initial_mtu, mtu_limit); |
| 5699 | writer_->set_max_packet_size(mtu_limit); |
| 5700 | |
| 5701 | const QuicPacketCount packets_between_probes_base = 5; |
| 5702 | set_packets_between_probes_base(packets_between_probes_base); |
| 5703 | |
| 5704 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5705 | |
| 5706 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5707 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5708 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5709 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5710 | } |
| 5711 | |
| 5712 | // Trigger the probe. |
| 5713 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5714 | nullptr); |
| 5715 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5716 | writer_->SimulateNextPacketTooLarge(); |
| 5717 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5718 | ASSERT_TRUE(connection_.connected()); |
| 5719 | |
| 5720 | // Send more data. |
| 5721 | QuicPacketNumber probe_number = creator_->packet_number(); |
| 5722 | QuicPacketCount extra_packets = packets_between_probes_base * 3; |
| 5723 | for (QuicPacketCount i = 0; i < extra_packets; i++) { |
| 5724 | connection_.EnsureWritableAndSendStreamData5(); |
| 5725 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5726 | } |
| 5727 | |
| 5728 | // Acknowledge all packets sent so far, except for the lost probe. |
| 5729 | QuicAckFrame probe_ack = |
| 5730 | ConstructAckFrame(creator_->packet_number(), probe_number); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5731 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5732 | ProcessAckPacket(&probe_ack); |
| 5733 | EXPECT_EQ(initial_mtu, connection_.max_packet_length()); |
| 5734 | |
| 5735 | // Send more packets, and ensure that none of them sets the alarm. |
| 5736 | for (QuicPacketCount i = 0; i < 4 * packets_between_probes_base; i++) { |
| 5737 | connection_.EnsureWritableAndSendStreamData5(); |
| 5738 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5739 | } |
| 5740 | |
| 5741 | EXPECT_EQ(initial_mtu, connection_.max_packet_length()); |
| 5742 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5743 | } |
| 5744 | |
| 5745 | TEST_P(QuicConnectionTest, NoMtuDiscoveryAfterConnectionClosed) { |
| 5746 | MtuDiscoveryTestInit(); |
| 5747 | |
| 5748 | const QuicPacketCount packets_between_probes_base = 10; |
| 5749 | set_packets_between_probes_base(packets_between_probes_base); |
| 5750 | |
| 5751 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5752 | |
| 5753 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5754 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5755 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5756 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5757 | } |
| 5758 | |
| 5759 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5760 | nullptr); |
| 5761 | EXPECT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5762 | |
| 5763 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 5764 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 5765 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 5766 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5767 | } |
| 5768 | |
| 5769 | TEST_P(QuicConnectionTest, TimeoutAfterSendDuringHandshake) { |
| 5770 | EXPECT_TRUE(connection_.connected()); |
| 5771 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5772 | QuicConfig config; |
| 5773 | connection_.SetFromConfig(config); |
| 5774 | |
| 5775 | const QuicTime::Delta initial_idle_timeout = |
| 5776 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 5777 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5778 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 5779 | |
| 5780 | // When we send a packet, the timeout will change to 5ms + |
| 5781 | // kInitialIdleTimeoutSecs. |
| 5782 | clock_.AdvanceTime(five_ms); |
| 5783 | SendStreamDataToPeer( |
| 5784 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5785 | 0, FIN, nullptr); |
| 5786 | EXPECT_EQ(default_timeout + five_ms, |
| 5787 | connection_.GetTimeoutAlarm()->deadline()); |
| 5788 | |
| 5789 | // Now send more data. This will not move the timeout because |
| 5790 | // no data has been received since the previous write. |
| 5791 | clock_.AdvanceTime(five_ms); |
| 5792 | SendStreamDataToPeer( |
| 5793 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5794 | 3, FIN, nullptr); |
| 5795 | EXPECT_EQ(default_timeout + five_ms, |
| 5796 | connection_.GetTimeoutAlarm()->deadline()); |
| 5797 | |
| 5798 | // The original alarm will fire. We should not time out because we had a |
| 5799 | // network event at t=5ms. The alarm will reregister. |
| 5800 | clock_.AdvanceTime(initial_idle_timeout - five_ms - five_ms); |
| 5801 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 5802 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5803 | EXPECT_TRUE(connection_.connected()); |
| 5804 | EXPECT_EQ(default_timeout + five_ms, |
| 5805 | connection_.GetTimeoutAlarm()->deadline()); |
| 5806 | |
| 5807 | // This time, we should time out. |
| 5808 | EXPECT_CALL(visitor_, |
| 5809 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 5810 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 5811 | clock_.AdvanceTime(five_ms); |
| 5812 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 5813 | connection_.GetTimeoutAlarm()->Fire(); |
| 5814 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5815 | EXPECT_FALSE(connection_.connected()); |
| 5816 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 5817 | } |
| 5818 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5819 | TEST_P(QuicConnectionTest, TimeoutAfterSendAfterHandshake) { |
| 5820 | // When the idle timeout fires, verify that by default we do not send any |
| 5821 | // connection close packets. |
| 5822 | EXPECT_TRUE(connection_.connected()); |
| 5823 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5824 | QuicConfig config; |
| 5825 | |
| 5826 | // Create a handshake message that also enables silent close. |
| 5827 | CryptoHandshakeMessage msg; |
| 5828 | std::string error_details; |
| 5829 | QuicConfig client_config; |
| 5830 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 5831 | kInitialStreamFlowControlWindowForTest); |
| 5832 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 5833 | kInitialSessionFlowControlWindowForTest); |
| 5834 | client_config.SetIdleNetworkTimeout( |
| 5835 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); |
| 5836 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
| 5837 | const QuicErrorCode error = |
| 5838 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
| 5839 | EXPECT_THAT(error, IsQuicNoError()); |
| 5840 | |
| 5841 | if (connection_.version().UsesTls()) { |
| 5842 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 5843 | &config, connection_.connection_id()); |
| 5844 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 5845 | &config, connection_.connection_id()); |
| 5846 | } |
| 5847 | connection_.SetFromConfig(config); |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 5848 | QuicConnectionPeer::DisableBandwidthUpdate(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5849 | |
| 5850 | const QuicTime::Delta default_idle_timeout = |
| 5851 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs - 1); |
| 5852 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5853 | QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; |
| 5854 | |
| 5855 | // When we send a packet, the timeout will change to 5ms + |
| 5856 | // kInitialIdleTimeoutSecs. |
| 5857 | clock_.AdvanceTime(five_ms); |
| 5858 | SendStreamDataToPeer( |
| 5859 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5860 | 0, FIN, nullptr); |
| 5861 | EXPECT_EQ(default_timeout + five_ms, |
| 5862 | connection_.GetTimeoutAlarm()->deadline()); |
| 5863 | |
| 5864 | // Now send more data. This will not move the timeout because |
| 5865 | // no data has been received since the previous write. |
| 5866 | clock_.AdvanceTime(five_ms); |
| 5867 | SendStreamDataToPeer( |
| 5868 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5869 | 3, FIN, nullptr); |
| 5870 | EXPECT_EQ(default_timeout + five_ms, |
| 5871 | connection_.GetTimeoutAlarm()->deadline()); |
| 5872 | |
| 5873 | // The original alarm will fire. We should not time out because we had a |
| 5874 | // network event at t=5ms. The alarm will reregister. |
| 5875 | clock_.AdvanceTime(default_idle_timeout - five_ms - five_ms); |
| 5876 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 5877 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5878 | EXPECT_TRUE(connection_.connected()); |
| 5879 | EXPECT_EQ(default_timeout + five_ms, |
| 5880 | connection_.GetTimeoutAlarm()->deadline()); |
| 5881 | |
| 5882 | // This time, we should time out. |
| 5883 | // This results in a SILENT_CLOSE, so the writer will not be invoked |
| 5884 | // and will not save the frame. Grab the frame from OnConnectionClosed |
| 5885 | // directly. |
| 5886 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 5887 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5888 | |
| 5889 | clock_.AdvanceTime(five_ms); |
| 5890 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 5891 | connection_.GetTimeoutAlarm()->Fire(); |
| 5892 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5893 | EXPECT_FALSE(connection_.connected()); |
| 5894 | EXPECT_EQ(1, connection_close_frame_count_); |
| 5895 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5896 | IsError(QUIC_NETWORK_IDLE_TIMEOUT)); |
| 5897 | } |
| 5898 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5899 | TEST_P(QuicConnectionTest, TimeoutAfterSendSilentCloseWithOpenStreams) { |
| 5900 | // Same test as above, but having open streams causes a connection close |
| 5901 | // to be sent. |
| 5902 | EXPECT_TRUE(connection_.connected()); |
| 5903 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5904 | QuicConfig config; |
| 5905 | |
| 5906 | // Create a handshake message that also enables silent close. |
| 5907 | CryptoHandshakeMessage msg; |
| 5908 | std::string error_details; |
| 5909 | QuicConfig client_config; |
| 5910 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 5911 | kInitialStreamFlowControlWindowForTest); |
| 5912 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 5913 | kInitialSessionFlowControlWindowForTest); |
| 5914 | client_config.SetIdleNetworkTimeout( |
| 5915 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); |
| 5916 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
| 5917 | const QuicErrorCode error = |
| 5918 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
| 5919 | EXPECT_THAT(error, IsQuicNoError()); |
| 5920 | |
| 5921 | if (connection_.version().UsesTls()) { |
| 5922 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 5923 | &config, connection_.connection_id()); |
| 5924 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 5925 | &config, connection_.connection_id()); |
| 5926 | } |
| 5927 | connection_.SetFromConfig(config); |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 5928 | QuicConnectionPeer::DisableBandwidthUpdate(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5929 | |
| 5930 | const QuicTime::Delta default_idle_timeout = |
| 5931 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs - 1); |
| 5932 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5933 | QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; |
| 5934 | |
| 5935 | // When we send a packet, the timeout will change to 5ms + |
| 5936 | // kInitialIdleTimeoutSecs. |
| 5937 | clock_.AdvanceTime(five_ms); |
| 5938 | SendStreamDataToPeer( |
| 5939 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5940 | 0, FIN, nullptr); |
| 5941 | EXPECT_EQ(default_timeout + five_ms, |
| 5942 | connection_.GetTimeoutAlarm()->deadline()); |
| 5943 | |
| 5944 | // Indicate streams are still open. |
| 5945 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5946 | .WillRepeatedly(Return(true)); |
| 5947 | if (GetQuicReloadableFlag(quic_add_stream_info_to_idle_close_detail)) { |
| 5948 | EXPECT_CALL(visitor_, GetStreamsInfoForLogging()).WillOnce(Return("")); |
| 5949 | } |
| 5950 | |
| 5951 | // This time, we should time out and send a connection close due to the TLP. |
| 5952 | EXPECT_CALL(visitor_, |
| 5953 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 5954 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 5955 | clock_.AdvanceTime(connection_.GetTimeoutAlarm()->deadline() - |
| 5956 | clock_.ApproximateNow() + five_ms); |
| 5957 | connection_.GetTimeoutAlarm()->Fire(); |
| 5958 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5959 | EXPECT_FALSE(connection_.connected()); |
| 5960 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 5961 | } |
| 5962 | |
| 5963 | TEST_P(QuicConnectionTest, TimeoutAfterReceive) { |
| 5964 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 5965 | EXPECT_TRUE(connection_.connected()); |
| 5966 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5967 | QuicConfig config; |
| 5968 | connection_.SetFromConfig(config); |
| 5969 | |
| 5970 | const QuicTime::Delta initial_idle_timeout = |
| 5971 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 5972 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5973 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 5974 | |
| 5975 | connection_.SendStreamDataWithString( |
| 5976 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5977 | 0, NO_FIN); |
| 5978 | connection_.SendStreamDataWithString( |
| 5979 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5980 | 3, NO_FIN); |
| 5981 | |
| 5982 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5983 | clock_.AdvanceTime(five_ms); |
| 5984 | |
| 5985 | // When we receive a packet, the timeout will change to 5ms + |
| 5986 | // kInitialIdleTimeoutSecs. |
| 5987 | QuicAckFrame ack = InitAckFrame(2); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 5988 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 5989 | ProcessAckPacket(&ack); |
| 5990 | |
| 5991 | // The original alarm will fire. We should not time out because we had a |
| 5992 | // network event at t=5ms. The alarm will reregister. |
| 5993 | clock_.AdvanceTime(initial_idle_timeout - five_ms); |
| 5994 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 5995 | EXPECT_TRUE(connection_.connected()); |
| 5996 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5997 | EXPECT_EQ(default_timeout + five_ms, |
| 5998 | connection_.GetTimeoutAlarm()->deadline()); |
| 5999 | |
| 6000 | // This time, we should time out. |
| 6001 | EXPECT_CALL(visitor_, |
| 6002 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 6003 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 6004 | clock_.AdvanceTime(five_ms); |
| 6005 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 6006 | connection_.GetTimeoutAlarm()->Fire(); |
| 6007 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6008 | EXPECT_FALSE(connection_.connected()); |
| 6009 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 6010 | } |
| 6011 | |
| 6012 | TEST_P(QuicConnectionTest, TimeoutAfterReceiveNotSendWhenUnacked) { |
| 6013 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6014 | EXPECT_TRUE(connection_.connected()); |
| 6015 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6016 | QuicConfig config; |
| 6017 | connection_.SetFromConfig(config); |
| 6018 | |
| 6019 | const QuicTime::Delta initial_idle_timeout = |
| 6020 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 6021 | connection_.SetNetworkTimeouts( |
| 6022 | QuicTime::Delta::Infinite(), |
| 6023 | initial_idle_timeout + QuicTime::Delta::FromSeconds(1)); |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 6024 | QuicConnectionPeer::DisableBandwidthUpdate(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6025 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 6026 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 6027 | |
| 6028 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6029 | connection_.SendStreamDataWithString( |
| 6030 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6031 | 0, NO_FIN); |
| 6032 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6033 | connection_.SendStreamDataWithString( |
| 6034 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6035 | 3, NO_FIN); |
| 6036 | |
| 6037 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 6038 | |
| 6039 | clock_.AdvanceTime(five_ms); |
| 6040 | |
| 6041 | // When we receive a packet, the timeout will change to 5ms + |
| 6042 | // kInitialIdleTimeoutSecs. |
| 6043 | QuicAckFrame ack = InitAckFrame(2); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 6044 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6045 | ProcessAckPacket(&ack); |
| 6046 | |
| 6047 | // The original alarm will fire. We should not time out because we had a |
| 6048 | // network event at t=5ms. The alarm will reregister. |
| 6049 | clock_.AdvanceTime(initial_idle_timeout - five_ms); |
| 6050 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 6051 | EXPECT_TRUE(connection_.connected()); |
| 6052 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6053 | EXPECT_EQ(default_timeout + five_ms, |
| 6054 | connection_.GetTimeoutAlarm()->deadline()); |
| 6055 | |
| 6056 | // Now, send packets while advancing the time and verify that the connection |
| 6057 | // eventually times out. |
| 6058 | EXPECT_CALL(visitor_, |
| 6059 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 6060 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 6061 | for (int i = 0; i < 100 && connection_.connected(); ++i) { |
| 6062 | QUIC_LOG(INFO) << "sending data packet"; |
| 6063 | connection_.SendStreamDataWithString( |
| 6064 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 6065 | "foo", 0, NO_FIN); |
| 6066 | connection_.GetTimeoutAlarm()->Fire(); |
| 6067 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 6068 | } |
| 6069 | EXPECT_FALSE(connection_.connected()); |
| 6070 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6071 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
| 6072 | } |
| 6073 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6074 | TEST_P(QuicConnectionTest, SendScheduler) { |
| 6075 | // Test that if we send a packet without delay, it is not queued. |
| 6076 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
| 6077 | std::unique_ptr<QuicPacket> packet = |
| 6078 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 6079 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6080 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6081 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
| 6082 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6083 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 6084 | } |
| 6085 | |
| 6086 | TEST_P(QuicConnectionTest, FailToSendFirstPacket) { |
| 6087 | // Test that the connection does not crash when it fails to send the first |
| 6088 | // packet at which point self_address_ might be uninitialized. |
| 6089 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
| 6090 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
| 6091 | std::unique_ptr<QuicPacket> packet = |
| 6092 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 6093 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6094 | writer_->SetShouldWriteFail(); |
| 6095 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
| 6096 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6097 | } |
| 6098 | |
| 6099 | TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) { |
| 6100 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
| 6101 | std::unique_ptr<QuicPacket> packet = |
| 6102 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 6103 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6104 | BlockOnNextWrite(); |
| 6105 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2u), _, _)) |
| 6106 | .Times(0); |
| 6107 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
| 6108 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6109 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 6110 | } |
| 6111 | |
| 6112 | TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) { |
| 6113 | // Queue the first packet. |
| 6114 | size_t payload_length = connection_.max_packet_length(); |
| 6115 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(testing::Return(false)); |
| 6116 | const std::string payload(payload_length, 'a'); |
| 6117 | QuicStreamId first_bidi_stream_id(QuicUtils::GetFirstBidirectionalStreamId( |
| 6118 | connection_.version().transport_version, Perspective::IS_CLIENT)); |
| 6119 | EXPECT_EQ(0u, connection_ |
| 6120 | .SendStreamDataWithString(first_bidi_stream_id, payload, 0, |
| 6121 | NO_FIN) |
| 6122 | .bytes_consumed); |
| 6123 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 6124 | } |
| 6125 | |
| 6126 | TEST_P(QuicConnectionTest, SendingThreePackets) { |
| 6127 | // Make the payload twice the size of the packet, so 3 packets are written. |
| 6128 | size_t total_payload_length = 2 * connection_.max_packet_length(); |
| 6129 | const std::string payload(total_payload_length, 'a'); |
| 6130 | QuicStreamId first_bidi_stream_id(QuicUtils::GetFirstBidirectionalStreamId( |
| 6131 | connection_.version().transport_version, Perspective::IS_CLIENT)); |
| 6132 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); |
| 6133 | EXPECT_EQ(payload.size(), connection_ |
| 6134 | .SendStreamDataWithString(first_bidi_stream_id, |
| 6135 | payload, 0, NO_FIN) |
| 6136 | .bytes_consumed); |
| 6137 | } |
| 6138 | |
| 6139 | TEST_P(QuicConnectionTest, LoopThroughSendingPacketsWithTruncation) { |
| 6140 | set_perspective(Perspective::IS_SERVER); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6141 | // Set up a larger payload than will fit in one packet. |
| 6142 | const std::string payload(connection_.max_packet_length(), 'a'); |
| 6143 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 6144 | |
| 6145 | // Now send some packets with no truncation. |
| 6146 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 6147 | EXPECT_EQ(payload.size(), |
| 6148 | connection_.SendStreamDataWithString(3, payload, 0, NO_FIN) |
| 6149 | .bytes_consumed); |
| 6150 | // Track the size of the second packet here. The overhead will be the largest |
| 6151 | // we see in this test, due to the non-truncated connection id. |
| 6152 | size_t non_truncated_packet_size = writer_->last_packet_size(); |
| 6153 | |
| 6154 | // Change to a 0 byte connection id. |
| 6155 | QuicConfig config; |
| 6156 | QuicConfigPeer::SetReceivedBytesForConnectionId(&config, 0); |
| 6157 | connection_.SetFromConfig(config); |
| 6158 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 6159 | EXPECT_EQ(payload.size(), |
| 6160 | connection_.SendStreamDataWithString(3, payload, 1350, NO_FIN) |
| 6161 | .bytes_consumed); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6162 | // Short header packets sent from server omit connection ID already, and |
| 6163 | // stream offset size increases from 0 to 2. |
| 6164 | EXPECT_EQ(non_truncated_packet_size, writer_->last_packet_size() - 2); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | TEST_P(QuicConnectionTest, SendDelayedAck) { |
| 6168 | QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6169 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6170 | EXPECT_FALSE(connection_.HasPendingAcks()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6171 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 6172 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 6173 | peer_framer_.SetEncrypter( |
| 6174 | ENCRYPTION_ZERO_RTT, |
| 6175 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6176 | // Process a packet from the non-crypto stream. |
| 6177 | frame1_.stream_id = 3; |
| 6178 | |
| 6179 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
| 6180 | // instead of ENCRYPTION_INITIAL. |
| 6181 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6182 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6183 | |
| 6184 | // Check if delayed ack timer is running for the expected interval. |
| 6185 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6186 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6187 | // Simulate delayed ack alarm firing. |
| 6188 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
| 6189 | connection_.GetAckAlarm()->Fire(); |
| 6190 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6191 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6192 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 6193 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6194 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6195 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6196 | } |
| 6197 | |
| 6198 | TEST_P(QuicConnectionTest, SendDelayedAckDecimation) { |
| 6199 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6200 | |
| 6201 | const size_t kMinRttMs = 40; |
| 6202 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6203 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6204 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6205 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6206 | // default delayed ack time. |
| 6207 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6208 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6209 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6210 | EXPECT_FALSE(connection_.HasPendingAcks()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6211 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 6212 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 6213 | peer_framer_.SetEncrypter( |
| 6214 | ENCRYPTION_ZERO_RTT, |
| 6215 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6216 | // Process a packet from the non-crypto stream. |
| 6217 | frame1_.stream_id = 3; |
| 6218 | |
| 6219 | // Process all the initial packets in order so there aren't missing packets. |
| 6220 | uint64_t kFirstDecimatedPacket = 101; |
| 6221 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6222 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6223 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6224 | } |
| 6225 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6226 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
| 6227 | // instead of ENCRYPTION_INITIAL. |
| 6228 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6229 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6230 | ENCRYPTION_ZERO_RTT); |
| 6231 | |
| 6232 | // Check if delayed ack timer is running for the expected interval. |
| 6233 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6234 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6235 | |
| 6236 | // The 10th received packet causes an ack to be sent. |
| 6237 | for (int i = 0; i < 9; ++i) { |
| 6238 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6239 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6240 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6241 | ENCRYPTION_ZERO_RTT); |
| 6242 | } |
| 6243 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6244 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6245 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 6246 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6247 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6248 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6249 | } |
| 6250 | |
| 6251 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationUnlimitedAggregation) { |
| 6252 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6253 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6254 | QuicConfig config; |
| 6255 | QuicTagVector connection_options; |
| 6256 | // No limit on the number of packets received before sending an ack. |
| 6257 | connection_options.push_back(kAKDU); |
| 6258 | config.SetConnectionOptionsToSend(connection_options); |
| 6259 | connection_.SetFromConfig(config); |
| 6260 | |
| 6261 | const size_t kMinRttMs = 40; |
| 6262 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6263 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6264 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6265 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6266 | // default delayed ack time. |
| 6267 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6268 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6269 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6270 | EXPECT_FALSE(connection_.HasPendingAcks()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6271 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 6272 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 6273 | peer_framer_.SetEncrypter( |
| 6274 | ENCRYPTION_ZERO_RTT, |
| 6275 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6276 | // Process a packet from the non-crypto stream. |
| 6277 | frame1_.stream_id = 3; |
| 6278 | |
| 6279 | // Process all the initial packets in order so there aren't missing packets. |
| 6280 | uint64_t kFirstDecimatedPacket = 101; |
| 6281 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6282 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6283 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6284 | } |
| 6285 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6286 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
| 6287 | // instead of ENCRYPTION_INITIAL. |
| 6288 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6289 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6290 | ENCRYPTION_ZERO_RTT); |
| 6291 | |
| 6292 | // Check if delayed ack timer is running for the expected interval. |
| 6293 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6294 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6295 | |
| 6296 | // 18 packets will not cause an ack to be sent. 19 will because when |
| 6297 | // stop waiting frames are in use, we ack every 20 packets no matter what. |
| 6298 | for (int i = 0; i < 18; ++i) { |
| 6299 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6300 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6301 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6302 | ENCRYPTION_ZERO_RTT); |
| 6303 | } |
| 6304 | // The delayed ack timer should still be set to the expected deadline. |
| 6305 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6306 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6307 | } |
| 6308 | |
| 6309 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationEighthRtt) { |
| 6310 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6311 | QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); |
| 6312 | |
| 6313 | const size_t kMinRttMs = 40; |
| 6314 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6315 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6316 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6317 | // The ack time should be based on min_rtt/8, since it's less than the |
| 6318 | // default delayed ack time. |
| 6319 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6320 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); |
| 6321 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6322 | EXPECT_FALSE(connection_.HasPendingAcks()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6323 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 6324 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 6325 | peer_framer_.SetEncrypter( |
| 6326 | ENCRYPTION_ZERO_RTT, |
| 6327 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6328 | // Process a packet from the non-crypto stream. |
| 6329 | frame1_.stream_id = 3; |
| 6330 | |
| 6331 | // Process all the initial packets in order so there aren't missing packets. |
| 6332 | uint64_t kFirstDecimatedPacket = 101; |
| 6333 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6334 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6335 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6336 | } |
| 6337 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6338 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
| 6339 | // instead of ENCRYPTION_INITIAL. |
| 6340 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6341 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6342 | ENCRYPTION_ZERO_RTT); |
| 6343 | |
| 6344 | // Check if delayed ack timer is running for the expected interval. |
| 6345 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6346 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6347 | |
| 6348 | // The 10th received packet causes an ack to be sent. |
| 6349 | for (int i = 0; i < 9; ++i) { |
| 6350 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6351 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6352 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6353 | ENCRYPTION_ZERO_RTT); |
| 6354 | } |
| 6355 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6356 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6357 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 6358 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6359 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6360 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6361 | } |
| 6362 | |
| 6363 | TEST_P(QuicConnectionTest, SendDelayedAckOnHandshakeConfirmed) { |
| 6364 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6365 | ProcessPacket(1); |
| 6366 | // Check that ack is sent and that delayed ack alarm is set. |
| 6367 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6368 | QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6369 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6370 | |
| 6371 | // Completing the handshake as the server does nothing. |
| 6372 | QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_SERVER); |
| 6373 | connection_.OnHandshakeComplete(); |
| 6374 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6375 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6376 | |
| 6377 | // Complete the handshake as the client decreases the delayed ack time to 0ms. |
| 6378 | QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_CLIENT); |
| 6379 | connection_.OnHandshakeComplete(); |
| 6380 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6381 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 6382 | EXPECT_EQ(clock_.ApproximateNow() + DefaultDelayedAckTime(), |
| 6383 | connection_.GetAckAlarm()->deadline()); |
| 6384 | } else { |
| 6385 | EXPECT_EQ(clock_.ApproximateNow(), connection_.GetAckAlarm()->deadline()); |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | TEST_P(QuicConnectionTest, SendDelayedAckOnSecondPacket) { |
| 6390 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6391 | ProcessPacket(1); |
| 6392 | ProcessPacket(2); |
| 6393 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6394 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6395 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 6396 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6397 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6398 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6399 | } |
| 6400 | |
| 6401 | TEST_P(QuicConnectionTest, NoAckOnOldNacks) { |
| 6402 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6403 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 6404 | ProcessPacket(2); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6405 | size_t frames_per_ack = 1; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6406 | |
| 6407 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 6408 | ProcessPacket(3); |
| 6409 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 6410 | EXPECT_EQ(padding_frame_count + frames_per_ack, writer_->frame_count()); |
| 6411 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6412 | writer_->Reset(); |
| 6413 | |
| 6414 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 6415 | ProcessPacket(4); |
| 6416 | EXPECT_EQ(0u, writer_->frame_count()); |
| 6417 | |
| 6418 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 6419 | ProcessPacket(5); |
| 6420 | padding_frame_count = writer_->padding_frames().size(); |
| 6421 | EXPECT_EQ(padding_frame_count + frames_per_ack, writer_->frame_count()); |
| 6422 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6423 | writer_->Reset(); |
| 6424 | |
| 6425 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 6426 | // Now only set the timer on the 6th packet, instead of sending another ack. |
| 6427 | ProcessPacket(6); |
| 6428 | padding_frame_count = writer_->padding_frames().size(); |
| 6429 | EXPECT_EQ(padding_frame_count, writer_->frame_count()); |
| 6430 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 6431 | } |
| 6432 | |
| 6433 | TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) { |
| 6434 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6435 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 6436 | peer_framer_.SetEncrypter( |
| 6437 | ENCRYPTION_FORWARD_SECURE, |
| 6438 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
| 6439 | SetDecrypter( |
| 6440 | ENCRYPTION_FORWARD_SECURE, |
| 6441 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6442 | ProcessDataPacket(1); |
| 6443 | connection_.SendStreamDataWithString( |
| 6444 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6445 | 0, NO_FIN); |
| 6446 | // Check that ack is bundled with outgoing data and that delayed ack |
| 6447 | // alarm is reset. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6448 | EXPECT_EQ(2u, writer_->frame_count()); |
| 6449 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6450 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 6451 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6452 | } |
| 6453 | |
| 6454 | TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingCryptoPacket) { |
| 6455 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6456 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6457 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 6458 | } else { |
| 6459 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6460 | } |
| 6461 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 6462 | connection_.SendCryptoDataWithString("foo", 0); |
| 6463 | // Check that ack is bundled with outgoing crypto data. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6464 | EXPECT_EQ(3u, writer_->frame_count()); |
| 6465 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6466 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6467 | } |
| 6468 | |
| 6469 | TEST_P(QuicConnectionTest, BlockAndBufferOnFirstCHLOPacketOfTwo) { |
| 6470 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6471 | ProcessPacket(1); |
| 6472 | BlockOnNextWrite(); |
| 6473 | writer_->set_is_write_blocked_data_buffered(true); |
| 6474 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6475 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 6476 | } else { |
| 6477 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 6478 | } |
| 6479 | connection_.SendCryptoDataWithString("foo", 0); |
| 6480 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 6481 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 6482 | connection_.SendCryptoDataWithString("bar", 3); |
| 6483 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 6484 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6485 | // CRYPTO frames are not flushed when writer is blocked. |
| 6486 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 6487 | } else { |
| 6488 | EXPECT_TRUE(connection_.HasQueuedData()); |
| 6489 | } |
| 6490 | } |
| 6491 | |
| 6492 | TEST_P(QuicConnectionTest, BundleAckForSecondCHLO) { |
| 6493 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6494 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6495 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 6496 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 6497 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 6498 | // Process a packet from the crypto stream, which is frame1_'s default. |
| 6499 | // Receiving the CHLO as packet 2 first will cause the connection to |
| 6500 | // immediately send an ack, due to the packet gap. |
| 6501 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6502 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 6503 | } else { |
| 6504 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6505 | } |
| 6506 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 6507 | // Check that ack is sent and that delayed ack alarm is reset. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6508 | EXPECT_EQ(3u, writer_->frame_count()); |
| 6509 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6510 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6511 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 6512 | } else { |
| 6513 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 6514 | } |
| 6515 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 6516 | ASSERT_FALSE(writer_->ack_frames().empty()); |
| 6517 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(writer_->ack_frames().front())); |
| 6518 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6519 | } |
| 6520 | |
| 6521 | TEST_P(QuicConnectionTest, BundleAckForSecondCHLOTwoPacketReject) { |
| 6522 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6523 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6524 | |
| 6525 | // Process two packets from the crypto stream, which is frame1_'s default, |
| 6526 | // simulating a 2 packet reject. |
| 6527 | { |
| 6528 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6529 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 6530 | } else { |
| 6531 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6532 | } |
| 6533 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 6534 | // Send the new CHLO when the REJ is processed. |
| 6535 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6536 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 6537 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 6538 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 6539 | } else { |
| 6540 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 6541 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 6542 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 6543 | } |
| 6544 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 6545 | } |
| 6546 | // Check that ack is sent and that delayed ack alarm is reset. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6547 | EXPECT_EQ(3u, writer_->frame_count()); |
| 6548 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6549 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 6550 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 6551 | } else { |
| 6552 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 6553 | } |
| 6554 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 6555 | ASSERT_FALSE(writer_->ack_frames().empty()); |
| 6556 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(writer_->ack_frames().front())); |
| 6557 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6558 | } |
| 6559 | |
| 6560 | TEST_P(QuicConnectionTest, BundleAckWithDataOnIncomingAck) { |
| 6561 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6562 | connection_.SendStreamDataWithString( |
| 6563 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6564 | 0, NO_FIN); |
| 6565 | connection_.SendStreamDataWithString( |
| 6566 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6567 | 3, NO_FIN); |
| 6568 | // Ack the second packet, which will retransmit the first packet. |
| 6569 | QuicAckFrame ack = ConstructAckFrame(2, 1); |
| 6570 | LostPacketVector lost_packets; |
| 6571 | lost_packets.push_back( |
| 6572 | LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize)); |
| 6573 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 6574 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 6575 | Return(LossDetectionInterface::DetectionStats()))); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 6576 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6577 | ProcessAckPacket(&ack); |
| 6578 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 6579 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 6580 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 6581 | writer_->Reset(); |
| 6582 | |
| 6583 | // Now ack the retransmission, which will both raise the high water mark |
| 6584 | // and see if there is more data to send. |
| 6585 | ack = ConstructAckFrame(3, 1); |
| 6586 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 6587 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6588 | ProcessAckPacket(&ack); |
| 6589 | |
| 6590 | // Check that no packet is sent and the ack alarm isn't set. |
| 6591 | EXPECT_EQ(0u, writer_->frame_count()); |
| 6592 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6593 | writer_->Reset(); |
| 6594 | |
| 6595 | // Send the same ack, but send both data and an ack together. |
| 6596 | ack = ConstructAckFrame(3, 1); |
| 6597 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 6598 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 6599 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 6600 | &connection_, &TestConnection::EnsureWritableAndSendStreamData5))); |
| 6601 | ProcessAckPacket(&ack); |
| 6602 | |
| 6603 | // Check that ack is bundled with outgoing data and the delayed ack |
| 6604 | // alarm is reset. |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6605 | // Do not ACK acks. |
| 6606 | EXPECT_EQ(1u, writer_->frame_count()); |
| 6607 | EXPECT_TRUE(writer_->ack_frames().empty()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6608 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 6609 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 6610 | } |
| 6611 | |
| 6612 | TEST_P(QuicConnectionTest, NoAckSentForClose) { |
| 6613 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6614 | ProcessPacket(1); |
| 6615 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 6616 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6617 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 6618 | ProcessClosePacket(2); |
| 6619 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6620 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6621 | IsError(QUIC_PEER_GOING_AWAY)); |
| 6622 | } |
| 6623 | |
| 6624 | TEST_P(QuicConnectionTest, SendWhenDisconnected) { |
| 6625 | EXPECT_TRUE(connection_.connected()); |
| 6626 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 6627 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6628 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 6629 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 6630 | EXPECT_FALSE(connection_.connected()); |
| 6631 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 6632 | EXPECT_EQ(DISCARD, connection_.GetSerializedPacketFate( |
| 6633 | /*is_mtu_discovery=*/false, ENCRYPTION_INITIAL)); |
| 6634 | } |
| 6635 | |
| 6636 | TEST_P(QuicConnectionTest, SendConnectivityProbingWhenDisconnected) { |
| 6637 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 6638 | if (!IsDefaultTestConfiguration()) { |
| 6639 | return; |
| 6640 | } |
| 6641 | |
| 6642 | EXPECT_TRUE(connection_.connected()); |
| 6643 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 6644 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6645 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 6646 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 6647 | EXPECT_FALSE(connection_.connected()); |
| 6648 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 6649 | |
| 6650 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 6651 | .Times(0); |
| 6652 | |
| 6653 | EXPECT_QUIC_BUG(connection_.SendConnectivityProbingPacket( |
| 6654 | writer_.get(), connection_.peer_address()), |
| 6655 | "Not sending connectivity probing packet as connection is " |
| 6656 | "disconnected."); |
| 6657 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6658 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6659 | IsError(QUIC_PEER_GOING_AWAY)); |
| 6660 | } |
| 6661 | |
| 6662 | TEST_P(QuicConnectionTest, WriteBlockedAfterClientSendsConnectivityProbe) { |
| 6663 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 6664 | TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 6665 | // Block next write so that sending connectivity probe will encounter a |
| 6666 | // blocked write when send a connectivity probe to the peer. |
| 6667 | probing_writer.BlockOnNextWrite(); |
| 6668 | // Connection will not be marked as write blocked as connectivity probe only |
| 6669 | // affects the probing_writer which is not the default. |
| 6670 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(0); |
| 6671 | |
| 6672 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 6673 | .Times(1); |
| 6674 | connection_.SendConnectivityProbingPacket(&probing_writer, |
| 6675 | connection_.peer_address()); |
| 6676 | } |
| 6677 | |
| 6678 | TEST_P(QuicConnectionTest, WriterBlockedAfterServerSendsConnectivityProbe) { |
| 6679 | PathProbeTestInit(Perspective::IS_SERVER); |
| 6680 | if (version().SupportsAntiAmplificationLimit()) { |
| 6681 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 6682 | } |
| 6683 | |
| 6684 | // Block next write so that sending connectivity probe will encounter a |
| 6685 | // blocked write when send a connectivity probe to the peer. |
| 6686 | writer_->BlockOnNextWrite(); |
| 6687 | // Connection will be marked as write blocked as server uses the default |
| 6688 | // writer to send connectivity probes. |
| 6689 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 6690 | |
| 6691 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 6692 | .Times(1); |
| 6693 | if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
| 6694 | QuicPathFrameBuffer payload{ |
| 6695 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 6696 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 6697 | connection_.SendPathChallenge( |
| 6698 | payload, connection_.self_address(), connection_.peer_address(), |
| 6699 | connection_.effective_peer_address(), writer_.get()); |
| 6700 | } else { |
| 6701 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 6702 | connection_.peer_address()); |
| 6703 | } |
| 6704 | } |
| 6705 | |
| 6706 | TEST_P(QuicConnectionTest, WriterErrorWhenClientSendsConnectivityProbe) { |
| 6707 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 6708 | TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 6709 | probing_writer.SetShouldWriteFail(); |
| 6710 | |
| 6711 | // Connection should not be closed if a connectivity probe is failed to be |
| 6712 | // sent. |
| 6713 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 6714 | |
| 6715 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 6716 | .Times(0); |
| 6717 | connection_.SendConnectivityProbingPacket(&probing_writer, |
| 6718 | connection_.peer_address()); |
| 6719 | } |
| 6720 | |
| 6721 | TEST_P(QuicConnectionTest, WriterErrorWhenServerSendsConnectivityProbe) { |
| 6722 | PathProbeTestInit(Perspective::IS_SERVER); |
| 6723 | |
| 6724 | writer_->SetShouldWriteFail(); |
| 6725 | // Connection should not be closed if a connectivity probe is failed to be |
| 6726 | // sent. |
| 6727 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 6728 | |
| 6729 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 6730 | .Times(0); |
| 6731 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 6732 | connection_.peer_address()); |
| 6733 | } |
| 6734 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6735 | TEST_P(QuicConnectionTest, IetfStatelessReset) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6736 | QuicConfig config; |
| 6737 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, |
| 6738 | kTestStatelessResetToken); |
| 6739 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6740 | connection_.SetFromConfig(config); |
| 6741 | std::unique_ptr<QuicEncryptedPacket> packet( |
| 6742 | QuicFramer::BuildIetfStatelessResetPacket(connection_id_, |
| 6743 | /*received_packet_length=*/100, |
| 6744 | kTestStatelessResetToken)); |
| 6745 | std::unique_ptr<QuicReceivedPacket> received( |
| 6746 | ConstructReceivedPacket(*packet, QuicTime::Zero())); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6747 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 6748 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6749 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
| 6750 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6751 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6752 | IsError(QUIC_PUBLIC_RESET)); |
| 6753 | } |
| 6754 | |
| 6755 | TEST_P(QuicConnectionTest, GoAway) { |
| 6756 | if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
| 6757 | // GoAway is not available in version 99. |
| 6758 | return; |
| 6759 | } |
| 6760 | |
| 6761 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6762 | |
| 6763 | QuicGoAwayFrame* goaway = new QuicGoAwayFrame(); |
| 6764 | goaway->last_good_stream_id = 1; |
| 6765 | goaway->error_code = QUIC_PEER_GOING_AWAY; |
| 6766 | goaway->reason_phrase = "Going away."; |
| 6767 | EXPECT_CALL(visitor_, OnGoAway(_)); |
| 6768 | ProcessGoAwayPacket(goaway); |
| 6769 | } |
| 6770 | |
| 6771 | TEST_P(QuicConnectionTest, WindowUpdate) { |
| 6772 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6773 | |
| 6774 | QuicWindowUpdateFrame window_update; |
| 6775 | window_update.stream_id = 3; |
| 6776 | window_update.max_data = 1234; |
| 6777 | EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); |
| 6778 | ProcessFramePacket(QuicFrame(window_update)); |
| 6779 | } |
| 6780 | |
| 6781 | TEST_P(QuicConnectionTest, Blocked) { |
| 6782 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6783 | |
| 6784 | QuicBlockedFrame blocked; |
| 6785 | blocked.stream_id = 3; |
| 6786 | EXPECT_CALL(visitor_, OnBlockedFrame(_)); |
| 6787 | ProcessFramePacket(QuicFrame(blocked)); |
| 6788 | EXPECT_EQ(1u, connection_.GetStats().blocked_frames_received); |
| 6789 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 6790 | } |
| 6791 | |
| 6792 | TEST_P(QuicConnectionTest, ZeroBytePacket) { |
| 6793 | // Don't close the connection for zero byte packets. |
| 6794 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 6795 | QuicReceivedPacket encrypted(nullptr, 0, QuicTime::Zero()); |
| 6796 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, encrypted); |
| 6797 | } |
| 6798 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6799 | TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) { |
| 6800 | // All supported versions except the one the connection supports. |
| 6801 | ParsedQuicVersionVector versions; |
| 6802 | for (auto version : AllSupportedVersions()) { |
| 6803 | if (version != connection_.version()) { |
| 6804 | versions.push_back(version); |
| 6805 | } |
| 6806 | } |
| 6807 | |
| 6808 | // Send a version negotiation packet. |
| 6809 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
| 6810 | QuicFramer::BuildVersionNegotiationPacket( |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6811 | connection_id_, EmptyQuicConnectionId(), /*ietf_quic=*/true, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6812 | connection_.version().HasLengthPrefixedConnectionIds(), versions)); |
| 6813 | std::unique_ptr<QuicReceivedPacket> received( |
| 6814 | ConstructReceivedPacket(*encrypted, QuicTime::Zero())); |
| 6815 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 6816 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6817 | // Verify no connection close packet gets sent. |
| 6818 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 6819 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
| 6820 | EXPECT_FALSE(connection_.connected()); |
| 6821 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6822 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6823 | IsError(QUIC_INVALID_VERSION)); |
| 6824 | } |
| 6825 | |
| 6826 | TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiationWithConnectionClose) { |
| 6827 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6828 | QuicConfig config; |
| 6829 | QuicTagVector connection_options; |
| 6830 | connection_options.push_back(kINVC); |
| 6831 | config.SetClientConnectionOptions(connection_options); |
| 6832 | connection_.SetFromConfig(config); |
| 6833 | |
| 6834 | // All supported versions except the one the connection supports. |
| 6835 | ParsedQuicVersionVector versions; |
| 6836 | for (auto version : AllSupportedVersions()) { |
| 6837 | if (version != connection_.version()) { |
| 6838 | versions.push_back(version); |
| 6839 | } |
| 6840 | } |
| 6841 | |
| 6842 | // Send a version negotiation packet. |
| 6843 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
| 6844 | QuicFramer::BuildVersionNegotiationPacket( |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6845 | connection_id_, EmptyQuicConnectionId(), /*ietf_quic=*/true, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6846 | connection_.version().HasLengthPrefixedConnectionIds(), versions)); |
| 6847 | std::unique_ptr<QuicReceivedPacket> received( |
| 6848 | ConstructReceivedPacket(*encrypted, QuicTime::Zero())); |
| 6849 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 6850 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6851 | // Verify connection close packet gets sent. |
| 6852 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1u)); |
| 6853 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
| 6854 | EXPECT_FALSE(connection_.connected()); |
| 6855 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6856 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6857 | IsError(QUIC_INVALID_VERSION)); |
| 6858 | } |
| 6859 | |
| 6860 | TEST_P(QuicConnectionTest, BadVersionNegotiation) { |
| 6861 | // Send a version negotiation packet with the version the client started with. |
| 6862 | // It should be rejected. |
| 6863 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 6864 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6865 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
| 6866 | QuicFramer::BuildVersionNegotiationPacket( |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6867 | connection_id_, EmptyQuicConnectionId(), /*ietf_quic=*/true, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6868 | connection_.version().HasLengthPrefixedConnectionIds(), |
| 6869 | AllSupportedVersions())); |
| 6870 | std::unique_ptr<QuicReceivedPacket> received( |
| 6871 | ConstructReceivedPacket(*encrypted, QuicTime::Zero())); |
| 6872 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
| 6873 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6874 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6875 | IsError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET)); |
| 6876 | } |
| 6877 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6878 | TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) { |
| 6879 | // Construct a packet with stream frame and connection close frame. |
| 6880 | QuicPacketHeader header; |
| 6881 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
| 6882 | header.source_connection_id = connection_id_; |
| 6883 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6884 | } else { |
| 6885 | header.destination_connection_id = connection_id_; |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6886 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6887 | } |
| 6888 | header.packet_number = QuicPacketNumber(1); |
| 6889 | header.version_flag = false; |
| 6890 | |
| 6891 | QuicErrorCode kQuicErrorCode = QUIC_PEER_GOING_AWAY; |
| 6892 | // This QuicConnectionCloseFrame will default to being for a Google QUIC |
| 6893 | // close. If doing IETF QUIC then set fields appropriately for CC/T or CC/A, |
| 6894 | // depending on the mapping. |
| 6895 | QuicConnectionCloseFrame qccf(peer_framer_.transport_version(), |
| 6896 | kQuicErrorCode, NO_IETF_QUIC_ERROR, "", |
| 6897 | /*transport_close_frame_type=*/0); |
| 6898 | QuicFrames frames; |
| 6899 | frames.push_back(QuicFrame(frame1_)); |
| 6900 | frames.push_back(QuicFrame(&qccf)); |
| 6901 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 6902 | EXPECT_TRUE(nullptr != packet); |
| 6903 | char buffer[kMaxOutgoingPacketSize]; |
| 6904 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 6905 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 6906 | kMaxOutgoingPacketSize); |
| 6907 | |
| 6908 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 6909 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 6910 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6911 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6912 | |
| 6913 | connection_.ProcessUdpPacket( |
| 6914 | kSelfAddress, kPeerAddress, |
| 6915 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
| 6916 | EXPECT_EQ(1, connection_close_frame_count_); |
| 6917 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 6918 | IsError(QUIC_PEER_GOING_AWAY)); |
| 6919 | } |
| 6920 | |
| 6921 | TEST_P(QuicConnectionTest, SelectMutualVersion) { |
| 6922 | connection_.SetSupportedVersions(AllSupportedVersions()); |
| 6923 | // Set the connection to speak the lowest quic version. |
| 6924 | connection_.set_version(QuicVersionMin()); |
| 6925 | EXPECT_EQ(QuicVersionMin(), connection_.version()); |
| 6926 | |
| 6927 | // Pass in available versions which includes a higher mutually supported |
| 6928 | // version. The higher mutually supported version should be selected. |
| 6929 | ParsedQuicVersionVector supported_versions = AllSupportedVersions(); |
| 6930 | EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); |
| 6931 | EXPECT_EQ(QuicVersionMax(), connection_.version()); |
| 6932 | |
| 6933 | // Expect that the lowest version is selected. |
| 6934 | // Ensure the lowest supported version is less than the max, unless they're |
| 6935 | // the same. |
| 6936 | ParsedQuicVersionVector lowest_version_vector; |
| 6937 | lowest_version_vector.push_back(QuicVersionMin()); |
| 6938 | EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); |
| 6939 | EXPECT_EQ(QuicVersionMin(), connection_.version()); |
| 6940 | |
| 6941 | // Shouldn't be able to find a mutually supported version. |
| 6942 | ParsedQuicVersionVector unsupported_version; |
| 6943 | unsupported_version.push_back(UnsupportedQuicVersion()); |
| 6944 | EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); |
| 6945 | } |
| 6946 | |
| 6947 | TEST_P(QuicConnectionTest, ConnectionCloseWhenWritable) { |
| 6948 | EXPECT_FALSE(writer_->IsWriteBlocked()); |
| 6949 | |
| 6950 | // Send a packet. |
| 6951 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 6952 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 6953 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 6954 | |
| 6955 | TriggerConnectionClose(); |
| 6956 | EXPECT_LE(2u, writer_->packets_write_attempts()); |
| 6957 | } |
| 6958 | |
| 6959 | TEST_P(QuicConnectionTest, ConnectionCloseGettingWriteBlocked) { |
| 6960 | BlockOnNextWrite(); |
| 6961 | TriggerConnectionClose(); |
| 6962 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 6963 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 6964 | } |
| 6965 | |
| 6966 | TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) { |
| 6967 | BlockOnNextWrite(); |
| 6968 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 6969 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 6970 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 6971 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 6972 | TriggerConnectionClose(); |
| 6973 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 6974 | } |
| 6975 | |
| 6976 | TEST_P(QuicConnectionTest, OnPacketSentDebugVisitor) { |
| 6977 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 6978 | MockQuicConnectionDebugVisitor debug_visitor; |
| 6979 | connection_.set_debug_visitor(&debug_visitor); |
| 6980 | |
| 6981 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(1); |
| 6982 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 6983 | |
| 6984 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(1); |
| 6985 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 6986 | connection_.peer_address()); |
| 6987 | } |
| 6988 | |
| 6989 | TEST_P(QuicConnectionTest, OnPacketHeaderDebugVisitor) { |
| 6990 | QuicPacketHeader header; |
| 6991 | header.packet_number = QuicPacketNumber(1); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 6992 | header.form = IETF_QUIC_LONG_HEADER_PACKET; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6993 | |
| 6994 | MockQuicConnectionDebugVisitor debug_visitor; |
| 6995 | connection_.set_debug_visitor(&debug_visitor); |
| 6996 | EXPECT_CALL(debug_visitor, OnPacketHeader(Ref(header), _, _)).Times(1); |
| 6997 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(1); |
| 6998 | EXPECT_CALL(debug_visitor, OnSuccessfulVersionNegotiation(_)).Times(1); |
| 6999 | connection_.OnPacketHeader(header); |
| 7000 | } |
| 7001 | |
| 7002 | TEST_P(QuicConnectionTest, Pacing) { |
| 7003 | TestConnection server(connection_id_, kPeerAddress, kSelfAddress, |
| 7004 | helper_.get(), alarm_factory_.get(), writer_.get(), |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 7005 | Perspective::IS_SERVER, version(), |
| 7006 | connection_id_generator_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7007 | TestConnection client(connection_id_, kSelfAddress, kPeerAddress, |
| 7008 | helper_.get(), alarm_factory_.get(), writer_.get(), |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 7009 | Perspective::IS_CLIENT, version(), |
| 7010 | connection_id_generator_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7011 | EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( |
| 7012 | static_cast<const QuicSentPacketManager*>( |
| 7013 | &client.sent_packet_manager()))); |
| 7014 | EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( |
| 7015 | static_cast<const QuicSentPacketManager*>( |
| 7016 | &server.sent_packet_manager()))); |
| 7017 | } |
| 7018 | |
| 7019 | TEST_P(QuicConnectionTest, WindowUpdateInstigateAcks) { |
| 7020 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7021 | |
| 7022 | // Send a WINDOW_UPDATE frame. |
| 7023 | QuicWindowUpdateFrame window_update; |
| 7024 | window_update.stream_id = 3; |
| 7025 | window_update.max_data = 1234; |
| 7026 | EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); |
| 7027 | ProcessFramePacket(QuicFrame(window_update)); |
| 7028 | |
| 7029 | // Ensure that this has caused the ACK alarm to be set. |
| 7030 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 7031 | } |
| 7032 | |
| 7033 | TEST_P(QuicConnectionTest, BlockedFrameInstigateAcks) { |
| 7034 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7035 | |
| 7036 | // Send a BLOCKED frame. |
| 7037 | QuicBlockedFrame blocked; |
| 7038 | blocked.stream_id = 3; |
| 7039 | EXPECT_CALL(visitor_, OnBlockedFrame(_)); |
| 7040 | ProcessFramePacket(QuicFrame(blocked)); |
| 7041 | |
| 7042 | // Ensure that this has caused the ACK alarm to be set. |
| 7043 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 7044 | } |
| 7045 | |
| 7046 | TEST_P(QuicConnectionTest, ReevaluateTimeUntilSendOnAck) { |
| 7047 | // Enable pacing. |
| 7048 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7049 | QuicConfig config; |
| 7050 | connection_.SetFromConfig(config); |
| 7051 | |
| 7052 | // Send two packets. One packet is not sufficient because if it gets acked, |
| 7053 | // there will be no packets in flight after that and the pacer will always |
| 7054 | // allow the next packet in that situation. |
| 7055 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7056 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 7057 | connection_.SendStreamDataWithString( |
| 7058 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 7059 | 0, NO_FIN); |
| 7060 | connection_.SendStreamDataWithString( |
| 7061 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "bar", |
| 7062 | 3, NO_FIN); |
| 7063 | connection_.OnCanWrite(); |
| 7064 | |
| 7065 | // Schedule the next packet for a few milliseconds in future. |
| 7066 | QuicSentPacketManagerPeer::DisablePacerBursts(manager_); |
| 7067 | QuicTime scheduled_pacing_time = |
| 7068 | clock_.Now() + QuicTime::Delta::FromMilliseconds(5); |
| 7069 | QuicSentPacketManagerPeer::SetNextPacedPacketTime(manager_, |
| 7070 | scheduled_pacing_time); |
| 7071 | |
| 7072 | // Send a packet and have it be blocked by congestion control. |
| 7073 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(false)); |
| 7074 | connection_.SendStreamDataWithString( |
| 7075 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "baz", |
| 7076 | 6, NO_FIN); |
| 7077 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 7078 | |
| 7079 | // Process an ack and the send alarm will be set to the new 5ms delay. |
| 7080 | QuicAckFrame ack = InitAckFrame(1); |
| 7081 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7082 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7083 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 7084 | ProcessAckPacket(&ack); |
| 7085 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 7086 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
| 7087 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 7088 | EXPECT_TRUE(connection_.GetSendAlarm()->IsSet()); |
| 7089 | EXPECT_EQ(scheduled_pacing_time, connection_.GetSendAlarm()->deadline()); |
| 7090 | writer_->Reset(); |
| 7091 | } |
| 7092 | |
| 7093 | TEST_P(QuicConnectionTest, SendAcksImmediately) { |
| 7094 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7095 | return; |
| 7096 | } |
| 7097 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7098 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7099 | ProcessDataPacket(1); |
| 7100 | CongestionBlockWrites(); |
| 7101 | SendAckPacketToPeer(); |
| 7102 | } |
| 7103 | |
| 7104 | TEST_P(QuicConnectionTest, SendPingImmediately) { |
| 7105 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7106 | connection_.set_debug_visitor(&debug_visitor); |
| 7107 | |
| 7108 | CongestionBlockWrites(); |
| 7109 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 7110 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7111 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(1); |
| 7112 | EXPECT_CALL(debug_visitor, OnPingSent()).Times(1); |
| 7113 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 7114 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7115 | } |
| 7116 | |
| 7117 | TEST_P(QuicConnectionTest, SendBlockedImmediately) { |
| 7118 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7119 | connection_.set_debug_visitor(&debug_visitor); |
| 7120 | |
| 7121 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 7122 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7123 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(1); |
| 7124 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
QUICHE team | 1a27108 | 2022-05-18 10:22:22 -0700 | [diff] [blame] | 7125 | connection_.SendControlFrame(QuicFrame(QuicBlockedFrame(1, 3, 0))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7126 | EXPECT_EQ(1u, connection_.GetStats().blocked_frames_sent); |
| 7127 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7128 | } |
| 7129 | |
| 7130 | TEST_P(QuicConnectionTest, FailedToSendBlockedFrames) { |
| 7131 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7132 | return; |
| 7133 | } |
| 7134 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7135 | connection_.set_debug_visitor(&debug_visitor); |
QUICHE team | 1a27108 | 2022-05-18 10:22:22 -0700 | [diff] [blame] | 7136 | QuicBlockedFrame blocked(1, 3, 0); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7137 | |
| 7138 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 7139 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(0); |
| 7140 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7141 | connection_.SendControlFrame(QuicFrame(blocked)); |
| 7142 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7143 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7144 | } |
| 7145 | |
| 7146 | TEST_P(QuicConnectionTest, SendingUnencryptedStreamDataFails) { |
| 7147 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 7148 | if (!IsDefaultTestConfiguration()) { |
| 7149 | return; |
| 7150 | } |
| 7151 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 7152 | EXPECT_QUIC_BUG( |
| 7153 | { |
| 7154 | EXPECT_CALL(visitor_, |
| 7155 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7156 | .WillOnce( |
| 7157 | Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 7158 | connection_.SaveAndSendStreamData(3, {}, 0, FIN); |
| 7159 | EXPECT_FALSE(connection_.connected()); |
| 7160 | EXPECT_EQ(1, connection_close_frame_count_); |
| 7161 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7162 | IsError(QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA)); |
| 7163 | }, |
| 7164 | "Cannot send stream data with level: ENCRYPTION_INITIAL"); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7165 | } |
| 7166 | |
| 7167 | TEST_P(QuicConnectionTest, SetRetransmissionAlarmForCryptoPacket) { |
| 7168 | EXPECT_TRUE(connection_.connected()); |
| 7169 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 7170 | |
| 7171 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7172 | connection_.SendCryptoStreamData(); |
| 7173 | |
| 7174 | // Verify retransmission timer is correctly set after crypto packet has been |
| 7175 | // sent. |
| 7176 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 7177 | QuicTime retransmission_time = |
| 7178 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7179 | ->GetRetransmissionTime(); |
| 7180 | EXPECT_NE(retransmission_time, clock_.ApproximateNow()); |
| 7181 | EXPECT_EQ(retransmission_time, |
| 7182 | connection_.GetRetransmissionAlarm()->deadline()); |
| 7183 | |
| 7184 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7185 | connection_.GetRetransmissionAlarm()->Fire(); |
| 7186 | } |
| 7187 | |
| 7188 | // Includes regression test for b/69979024. |
| 7189 | TEST_P(QuicConnectionTest, PathDegradingDetectionForNonCryptoPackets) { |
| 7190 | EXPECT_TRUE(connection_.connected()); |
| 7191 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7192 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7193 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 7194 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 7195 | connection_.OnHandshakeComplete(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7196 | |
| 7197 | const char data[] = "data"; |
| 7198 | size_t data_size = strlen(data); |
| 7199 | QuicStreamOffset offset = 0; |
| 7200 | |
| 7201 | for (int i = 0; i < 2; ++i) { |
| 7202 | // Send a packet. Now there's a retransmittable packet on the wire, so the |
| 7203 | // path degrading detection should be set. |
| 7204 | connection_.SendStreamDataWithString( |
| 7205 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), data, |
| 7206 | offset, NO_FIN); |
| 7207 | offset += data_size; |
| 7208 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7209 | // Check the deadline of the path degrading detection. |
| 7210 | QuicTime::Delta delay = |
| 7211 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7212 | ->GetPathDegradingDelay(); |
| 7213 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7214 | clock_.ApproximateNow()); |
| 7215 | |
| 7216 | // Send a second packet. The path degrading detection's deadline should |
| 7217 | // remain the same. |
| 7218 | // Regression test for b/69979024. |
| 7219 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7220 | QuicTime prev_deadline = |
| 7221 | connection_.GetBlackholeDetectorAlarm()->deadline(); |
| 7222 | connection_.SendStreamDataWithString( |
| 7223 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), data, |
| 7224 | offset, NO_FIN); |
| 7225 | offset += data_size; |
| 7226 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7227 | EXPECT_EQ(prev_deadline, |
| 7228 | connection_.GetBlackholeDetectorAlarm()->deadline()); |
| 7229 | |
| 7230 | // Now receive an ACK of the first packet. This should advance the path |
| 7231 | // degrading detection's deadline since forward progress has been made. |
| 7232 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7233 | if (i == 0) { |
| 7234 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7235 | } |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7236 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7237 | QuicAckFrame frame = InitAckFrame( |
| 7238 | {{QuicPacketNumber(1u + 2u * i), QuicPacketNumber(2u + 2u * i)}}); |
| 7239 | ProcessAckPacket(&frame); |
| 7240 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7241 | // Check the deadline of the path degrading detection. |
| 7242 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7243 | ->GetPathDegradingDelay(); |
| 7244 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7245 | clock_.ApproximateNow()); |
| 7246 | |
| 7247 | if (i == 0) { |
| 7248 | // Now receive an ACK of the second packet. Since there are no more |
| 7249 | // retransmittable packets on the wire, this should cancel the path |
| 7250 | // degrading detection. |
| 7251 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7252 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7253 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 7254 | ProcessAckPacket(&frame); |
| 7255 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7256 | } else { |
| 7257 | // Advance time to the path degrading alarm's deadline and simulate |
| 7258 | // firing the alarm. |
| 7259 | clock_.AdvanceTime(delay); |
| 7260 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 7261 | connection_.PathDegradingTimeout(); |
| 7262 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7263 | } |
| 7264 | } |
| 7265 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 7266 | } |
| 7267 | |
| 7268 | TEST_P(QuicConnectionTest, RetransmittableOnWireSetsPingAlarm) { |
| 7269 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 7270 | QuicTime::Delta::FromMilliseconds(50); |
| 7271 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 7272 | retransmittable_on_wire_timeout); |
| 7273 | |
| 7274 | EXPECT_TRUE(connection_.connected()); |
| 7275 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 7276 | .WillRepeatedly(Return(true)); |
| 7277 | |
| 7278 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7279 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7280 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7281 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 7282 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 7283 | connection_.OnHandshakeComplete(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7284 | |
| 7285 | const char data[] = "data"; |
| 7286 | size_t data_size = strlen(data); |
| 7287 | QuicStreamOffset offset = 0; |
| 7288 | |
| 7289 | // Send a packet. |
| 7290 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7291 | offset += data_size; |
| 7292 | // Now there's a retransmittable packet on the wire, so the path degrading |
| 7293 | // alarm should be set. |
| 7294 | // The retransmittable-on-wire alarm should not be set. |
| 7295 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7296 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7297 | ->GetPathDegradingDelay(); |
| 7298 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7299 | clock_.ApproximateNow()); |
| 7300 | ASSERT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 7301 | // The ping alarm is set for the ping timeout, not the shorter |
| 7302 | // retransmittable_on_wire_timeout. |
| 7303 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7304 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
| 7305 | EXPECT_EQ(ping_delay, |
| 7306 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7307 | |
| 7308 | // Now receive an ACK of the packet. |
| 7309 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7310 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7311 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7312 | QuicAckFrame frame = |
| 7313 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 7314 | ProcessAckPacket(&frame); |
| 7315 | // No more retransmittable packets on the wire, so the path degrading alarm |
| 7316 | // should be cancelled, and the ping alarm should be set to the |
| 7317 | // retransmittable_on_wire_timeout. |
| 7318 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7319 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7320 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 7321 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7322 | |
| 7323 | // Simulate firing the ping alarm and sending a PING. |
| 7324 | clock_.AdvanceTime(retransmittable_on_wire_timeout); |
| 7325 | connection_.GetPingAlarm()->Fire(); |
| 7326 | |
| 7327 | // Now there's a retransmittable packet (PING) on the wire, so the path |
| 7328 | // degrading alarm should be set. |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7329 | ASSERT_TRUE(connection_.PathDegradingDetectionInProgress()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7330 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7331 | ->GetPathDegradingDelay(); |
| 7332 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7333 | clock_.ApproximateNow()); |
| 7334 | } |
| 7335 | |
| 7336 | TEST_P(QuicConnectionTest, ServerRetransmittableOnWire) { |
| 7337 | set_perspective(Perspective::IS_SERVER); |
| 7338 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 7339 | SetQuicReloadableFlag(quic_enable_server_on_wire_ping, true); |
| 7340 | |
| 7341 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7342 | QuicConfig config; |
| 7343 | QuicTagVector connection_options; |
| 7344 | connection_options.push_back(kSRWP); |
| 7345 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 7346 | connection_.SetFromConfig(config); |
| 7347 | |
| 7348 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 7349 | .WillRepeatedly(Return(true)); |
| 7350 | |
| 7351 | ProcessPacket(1); |
| 7352 | |
| 7353 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7354 | QuicTime::Delta ping_delay = QuicTime::Delta::FromMilliseconds(200); |
| 7355 | EXPECT_EQ(ping_delay, |
| 7356 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7357 | |
| 7358 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 7359 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 7360 | // Verify PING alarm gets cancelled. |
| 7361 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 7362 | |
| 7363 | // Now receive an ACK of the packet. |
| 7364 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7365 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7366 | QuicAckFrame frame = |
| 7367 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 7368 | ProcessAckPacket(2, &frame); |
| 7369 | // Verify PING alarm gets scheduled. |
| 7370 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7371 | EXPECT_EQ(ping_delay, |
| 7372 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7373 | } |
| 7374 | |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7375 | TEST_P(QuicConnectionTest, RetransmittableOnWireSendFirstPacket) { |
fayang | 9b45510 | 2022-11-29 18:19:20 -0800 | [diff] [blame] | 7376 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7377 | return; |
| 7378 | } |
| 7379 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 7380 | .WillRepeatedly(Return(true)); |
| 7381 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7382 | |
| 7383 | const QuicTime::Delta kRetransmittableOnWireTimeout = |
| 7384 | QuicTime::Delta::FromMilliseconds(200); |
| 7385 | const QuicTime::Delta kTestRtt = QuicTime::Delta::FromMilliseconds(100); |
| 7386 | |
| 7387 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 7388 | kRetransmittableOnWireTimeout); |
| 7389 | |
| 7390 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7391 | QuicConfig config; |
| 7392 | QuicTagVector connection_options; |
| 7393 | connection_options.push_back(kROWF); |
| 7394 | config.SetClientConnectionOptions(connection_options); |
| 7395 | connection_.SetFromConfig(config); |
| 7396 | |
| 7397 | // Send a request. |
| 7398 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7399 | // Receive an ACK after 1-RTT. |
| 7400 | clock_.AdvanceTime(kTestRtt); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7401 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7402 | QuicAckFrame frame = |
| 7403 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 7404 | ProcessAckPacket(&frame); |
| 7405 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7406 | EXPECT_EQ(kRetransmittableOnWireTimeout, |
| 7407 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7408 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7409 | |
| 7410 | // Fire retransmittable-on-wire alarm. |
| 7411 | clock_.AdvanceTime(kRetransmittableOnWireTimeout); |
| 7412 | connection_.GetPingAlarm()->Fire(); |
| 7413 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 7414 | // Verify alarm is set in keep-alive mode. |
| 7415 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7416 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
| 7417 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7418 | } |
| 7419 | |
| 7420 | TEST_P(QuicConnectionTest, RetransmittableOnWireSendRandomBytes) { |
fayang | 9b45510 | 2022-11-29 18:19:20 -0800 | [diff] [blame] | 7421 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7422 | return; |
| 7423 | } |
| 7424 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 7425 | .WillRepeatedly(Return(true)); |
| 7426 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7427 | |
| 7428 | const QuicTime::Delta kRetransmittableOnWireTimeout = |
| 7429 | QuicTime::Delta::FromMilliseconds(200); |
| 7430 | const QuicTime::Delta kTestRtt = QuicTime::Delta::FromMilliseconds(100); |
| 7431 | |
| 7432 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 7433 | kRetransmittableOnWireTimeout); |
| 7434 | |
| 7435 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7436 | QuicConfig config; |
| 7437 | QuicTagVector connection_options; |
| 7438 | connection_options.push_back(kROWR); |
| 7439 | config.SetClientConnectionOptions(connection_options); |
| 7440 | connection_.SetFromConfig(config); |
| 7441 | |
| 7442 | // Send a request. |
| 7443 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7444 | // Receive an ACK after 1-RTT. |
| 7445 | clock_.AdvanceTime(kTestRtt); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7446 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7447 | QuicAckFrame frame = |
| 7448 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 7449 | ProcessAckPacket(&frame); |
| 7450 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7451 | EXPECT_EQ(kRetransmittableOnWireTimeout, |
| 7452 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7453 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7454 | |
| 7455 | // Fire retransmittable-on-wire alarm. |
| 7456 | clock_.AdvanceTime(kRetransmittableOnWireTimeout); |
| 7457 | // Next packet is not processable by the framer in the test writer. |
| 7458 | ExpectNextPacketUnprocessable(); |
| 7459 | connection_.GetPingAlarm()->Fire(); |
| 7460 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 7461 | // Verify alarm is set in keep-alive mode. |
| 7462 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7463 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
| 7464 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7465 | } |
| 7466 | |
| 7467 | TEST_P(QuicConnectionTest, |
| 7468 | RetransmittableOnWireSendRandomBytesWithWriterBlocked) { |
fayang | 9b45510 | 2022-11-29 18:19:20 -0800 | [diff] [blame] | 7469 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7470 | return; |
| 7471 | } |
| 7472 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 7473 | .WillRepeatedly(Return(true)); |
| 7474 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7475 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7476 | |
| 7477 | const QuicTime::Delta kRetransmittableOnWireTimeout = |
| 7478 | QuicTime::Delta::FromMilliseconds(200); |
| 7479 | const QuicTime::Delta kTestRtt = QuicTime::Delta::FromMilliseconds(100); |
| 7480 | |
| 7481 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 7482 | kRetransmittableOnWireTimeout); |
| 7483 | |
| 7484 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7485 | QuicConfig config; |
| 7486 | QuicTagVector connection_options; |
| 7487 | connection_options.push_back(kROWR); |
| 7488 | config.SetClientConnectionOptions(connection_options); |
| 7489 | connection_.SetFromConfig(config); |
| 7490 | |
| 7491 | // Send a request. |
| 7492 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7493 | // Receive an ACK after 1-RTT. |
| 7494 | clock_.AdvanceTime(kTestRtt); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7495 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
fayang | f6607db | 2022-04-21 18:10:41 -0700 | [diff] [blame] | 7496 | QuicAckFrame frame = |
| 7497 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 7498 | ProcessAckPacket(&frame); |
| 7499 | ASSERT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 7500 | EXPECT_EQ(kRetransmittableOnWireTimeout, |
| 7501 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 7502 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7503 | // Receive an out of order data packet and block the ACK packet. |
| 7504 | BlockOnNextWrite(); |
| 7505 | ProcessDataPacket(3); |
| 7506 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 7507 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 7508 | |
| 7509 | // Fire retransmittable-on-wire alarm. |
| 7510 | clock_.AdvanceTime(kRetransmittableOnWireTimeout); |
| 7511 | connection_.GetPingAlarm()->Fire(); |
| 7512 | // Verify the random bytes packet gets queued. |
| 7513 | EXPECT_EQ(2u, connection_.NumQueuedPackets()); |
| 7514 | } |
| 7515 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7516 | // This test verifies that the connection marks path as degrading and does not |
| 7517 | // spin timer to detect path degrading when a new packet is sent on the |
| 7518 | // degraded path. |
| 7519 | TEST_P(QuicConnectionTest, NoPathDegradingDetectionIfPathIsDegrading) { |
| 7520 | EXPECT_TRUE(connection_.connected()); |
| 7521 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7522 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7523 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 7524 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 7525 | connection_.OnHandshakeComplete(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7526 | |
| 7527 | const char data[] = "data"; |
| 7528 | size_t data_size = strlen(data); |
| 7529 | QuicStreamOffset offset = 0; |
| 7530 | |
| 7531 | // Send the first packet. Now there's a retransmittable packet on the wire, so |
| 7532 | // the path degrading alarm should be set. |
| 7533 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7534 | offset += data_size; |
| 7535 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7536 | // Check the deadline of the path degrading detection. |
| 7537 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7538 | ->GetPathDegradingDelay(); |
| 7539 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7540 | clock_.ApproximateNow()); |
| 7541 | |
| 7542 | // Send a second packet. The path degrading detection's deadline should remain |
| 7543 | // the same. |
| 7544 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7545 | QuicTime prev_deadline = connection_.GetBlackholeDetectorAlarm()->deadline(); |
| 7546 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7547 | offset += data_size; |
| 7548 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7549 | EXPECT_EQ(prev_deadline, connection_.GetBlackholeDetectorAlarm()->deadline()); |
| 7550 | |
| 7551 | // Now receive an ACK of the first packet. This should advance the path |
| 7552 | // degrading detection's deadline since forward progress has been made. |
| 7553 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7554 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7555 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7556 | QuicAckFrame frame = |
| 7557 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 7558 | ProcessAckPacket(&frame); |
| 7559 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7560 | // Check the deadline of the path degrading alarm. |
| 7561 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7562 | ->GetPathDegradingDelay(); |
| 7563 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7564 | clock_.ApproximateNow()); |
| 7565 | |
| 7566 | // Advance time to the path degrading detection's deadline and simulate |
| 7567 | // firing the path degrading detection. This path will be considered as |
| 7568 | // degrading. |
| 7569 | clock_.AdvanceTime(delay); |
| 7570 | EXPECT_CALL(visitor_, OnPathDegrading()).Times(1); |
| 7571 | connection_.PathDegradingTimeout(); |
| 7572 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7573 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 7574 | |
| 7575 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7576 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7577 | // Send a third packet. The path degrading detection is no longer set but path |
| 7578 | // should still be marked as degrading. |
| 7579 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7580 | offset += data_size; |
| 7581 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7582 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 7583 | } |
| 7584 | |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7585 | TEST_P(QuicConnectionTest, NoPathDegradingDetectionBeforeHandshakeConfirmed) { |
| 7586 | EXPECT_TRUE(connection_.connected()); |
| 7587 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7588 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7589 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 7590 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 7591 | |
| 7592 | connection_.SendStreamDataWithString(1, "data", 0, NO_FIN); |
| 7593 | if (GetQuicReloadableFlag( |
| 7594 | quic_no_path_degrading_before_handshake_confirmed) && |
| 7595 | connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7596 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7597 | } else { |
| 7598 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7599 | } |
| 7600 | } |
| 7601 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7602 | // This test verifies that the connection unmarks path as degrarding and spins |
| 7603 | // the timer to detect future path degrading when forward progress is made |
| 7604 | // after path has been marked degrading. |
| 7605 | TEST_P(QuicConnectionTest, UnmarkPathDegradingOnForwardProgress) { |
| 7606 | EXPECT_TRUE(connection_.connected()); |
| 7607 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7608 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 7609 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 7610 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 7611 | connection_.OnHandshakeComplete(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7612 | |
| 7613 | const char data[] = "data"; |
| 7614 | size_t data_size = strlen(data); |
| 7615 | QuicStreamOffset offset = 0; |
| 7616 | |
| 7617 | // Send the first packet. Now there's a retransmittable packet on the wire, so |
| 7618 | // the path degrading alarm should be set. |
| 7619 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7620 | offset += data_size; |
| 7621 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7622 | // Check the deadline of the path degrading alarm. |
| 7623 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7624 | ->GetPathDegradingDelay(); |
| 7625 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7626 | clock_.ApproximateNow()); |
| 7627 | |
| 7628 | // Send a second packet. The path degrading alarm's deadline should remain |
| 7629 | // the same. |
| 7630 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7631 | QuicTime prev_deadline = connection_.GetBlackholeDetectorAlarm()->deadline(); |
| 7632 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7633 | offset += data_size; |
| 7634 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7635 | EXPECT_EQ(prev_deadline, connection_.GetBlackholeDetectorAlarm()->deadline()); |
| 7636 | |
| 7637 | // Now receive an ACK of the first packet. This should advance the path |
| 7638 | // degrading alarm's deadline since forward progress has been made. |
| 7639 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7640 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7641 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7642 | QuicAckFrame frame = |
| 7643 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 7644 | ProcessAckPacket(&frame); |
| 7645 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7646 | // Check the deadline of the path degrading alarm. |
| 7647 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7648 | ->GetPathDegradingDelay(); |
| 7649 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7650 | clock_.ApproximateNow()); |
| 7651 | |
| 7652 | // Advance time to the path degrading alarm's deadline and simulate |
| 7653 | // firing the alarm. |
| 7654 | clock_.AdvanceTime(delay); |
| 7655 | EXPECT_CALL(visitor_, OnPathDegrading()).Times(1); |
| 7656 | connection_.PathDegradingTimeout(); |
| 7657 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7658 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 7659 | |
| 7660 | // Send a third packet. The path degrading alarm is no longer set but path |
| 7661 | // should still be marked as degrading. |
| 7662 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7663 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7664 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 7665 | offset += data_size; |
| 7666 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7667 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 7668 | |
| 7669 | // Now receive an ACK of the second packet. This should unmark the path as |
| 7670 | // degrading. And will set a timer to detect new path degrading. |
| 7671 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7672 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7673 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()).Times(1); |
| 7674 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 7675 | ProcessAckPacket(&frame); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 7676 | EXPECT_EQ(1, |
| 7677 | connection_.GetStats().num_forward_progress_after_path_degrading); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7678 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7679 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 7680 | } |
| 7681 | |
| 7682 | TEST_P(QuicConnectionTest, NoPathDegradingOnServer) { |
| 7683 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7684 | return; |
| 7685 | } |
| 7686 | set_perspective(Perspective::IS_SERVER); |
| 7687 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 7688 | |
| 7689 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7690 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7691 | |
| 7692 | // Send data. |
| 7693 | const char data[] = "data"; |
| 7694 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 7695 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7696 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7697 | |
| 7698 | // Ack data. |
| 7699 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 7700 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7701 | QuicAckFrame frame = |
| 7702 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 7703 | ProcessAckPacket(&frame); |
| 7704 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7705 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7706 | } |
| 7707 | |
| 7708 | TEST_P(QuicConnectionTest, NoPathDegradingAfterSendingAck) { |
| 7709 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7710 | return; |
| 7711 | } |
| 7712 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7713 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7714 | ProcessDataPacket(1); |
| 7715 | SendAckPacketToPeer(); |
| 7716 | EXPECT_FALSE(connection_.sent_packet_manager().unacked_packets().empty()); |
| 7717 | EXPECT_FALSE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 7718 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7719 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 7720 | } |
| 7721 | |
| 7722 | TEST_P(QuicConnectionTest, MultipleCallsToCloseConnection) { |
| 7723 | // Verifies that multiple calls to CloseConnection do not |
| 7724 | // result in multiple attempts to close the connection - it will be marked as |
| 7725 | // disconnected after the first call. |
| 7726 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
| 7727 | connection_.CloseConnection(QUIC_NO_ERROR, "no reason", |
| 7728 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 7729 | connection_.CloseConnection(QUIC_NO_ERROR, "no reason", |
| 7730 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 7731 | } |
| 7732 | |
| 7733 | TEST_P(QuicConnectionTest, ServerReceivesChloOnNonCryptoStream) { |
| 7734 | set_perspective(Perspective::IS_SERVER); |
| 7735 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 7736 | QuicConnectionPeer::SetAddressValidated(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7737 | |
| 7738 | CryptoHandshakeMessage message; |
| 7739 | CryptoFramer framer; |
| 7740 | message.set_tag(kCHLO); |
| 7741 | std::unique_ptr<QuicData> data = framer.ConstructHandshakeMessage(message); |
| 7742 | frame1_.stream_id = 10; |
| 7743 | frame1_.data_buffer = data->data(); |
| 7744 | frame1_.data_length = data->length(); |
| 7745 | |
| 7746 | if (version().handshake_protocol == PROTOCOL_TLS1_3) { |
| 7747 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 7748 | } |
| 7749 | EXPECT_CALL(visitor_, |
| 7750 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 7751 | ForceProcessFramePacket(QuicFrame(frame1_)); |
| 7752 | if (VersionHasIetfQuicFrames(version().transport_version)) { |
| 7753 | // INITIAL packet should not contain STREAM frame. |
| 7754 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 7755 | } else { |
| 7756 | TestConnectionCloseQuicErrorCode(QUIC_MAYBE_CORRUPTED_MEMORY); |
| 7757 | } |
| 7758 | } |
| 7759 | |
| 7760 | TEST_P(QuicConnectionTest, ClientReceivesRejOnNonCryptoStream) { |
| 7761 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7762 | |
| 7763 | CryptoHandshakeMessage message; |
| 7764 | CryptoFramer framer; |
| 7765 | message.set_tag(kREJ); |
| 7766 | std::unique_ptr<QuicData> data = framer.ConstructHandshakeMessage(message); |
| 7767 | frame1_.stream_id = 10; |
| 7768 | frame1_.data_buffer = data->data(); |
| 7769 | frame1_.data_length = data->length(); |
| 7770 | |
| 7771 | EXPECT_CALL(visitor_, |
| 7772 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 7773 | ForceProcessFramePacket(QuicFrame(frame1_)); |
| 7774 | if (VersionHasIetfQuicFrames(version().transport_version)) { |
| 7775 | // INITIAL packet should not contain STREAM frame. |
| 7776 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 7777 | } else { |
| 7778 | TestConnectionCloseQuicErrorCode(QUIC_MAYBE_CORRUPTED_MEMORY); |
| 7779 | } |
| 7780 | } |
| 7781 | |
| 7782 | TEST_P(QuicConnectionTest, CloseConnectionOnPacketTooLarge) { |
| 7783 | SimulateNextPacketTooLarge(); |
| 7784 | // A connection close packet is sent |
| 7785 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7786 | .Times(1); |
| 7787 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7788 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 7789 | } |
| 7790 | |
| 7791 | TEST_P(QuicConnectionTest, AlwaysGetPacketTooLarge) { |
| 7792 | // Test even we always get packet too large, we do not infinitely try to send |
| 7793 | // close packet. |
| 7794 | AlwaysGetPacketTooLarge(); |
| 7795 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7796 | .Times(1); |
| 7797 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7798 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 7799 | } |
| 7800 | |
| 7801 | TEST_P(QuicConnectionTest, CloseConnectionOnQueuedWriteError) { |
| 7802 | // Regression test for crbug.com/979507. |
| 7803 | // |
| 7804 | // If we get a write error when writing queued packets, we should attempt to |
| 7805 | // send a connection close packet, but if sending that fails, it shouldn't get |
| 7806 | // queued. |
| 7807 | |
| 7808 | // Queue a packet to write. |
| 7809 | BlockOnNextWrite(); |
| 7810 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7811 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 7812 | |
| 7813 | // Configure writer to always fail. |
| 7814 | AlwaysGetPacketTooLarge(); |
| 7815 | |
| 7816 | // Expect that we attempt to close the connection exactly once. |
| 7817 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7818 | .Times(1); |
| 7819 | |
| 7820 | // Unblock the writes and actually send. |
| 7821 | writer_->SetWritable(); |
| 7822 | connection_.OnCanWrite(); |
| 7823 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 7824 | |
| 7825 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 7826 | } |
| 7827 | |
| 7828 | // Verify that if connection has no outstanding data, it notifies the send |
| 7829 | // algorithm after the write. |
| 7830 | TEST_P(QuicConnectionTest, SendDataAndBecomeApplicationLimited) { |
| 7831 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(1); |
| 7832 | { |
| 7833 | InSequence seq; |
| 7834 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 7835 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 7836 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 7837 | .WillRepeatedly(Return(false)); |
| 7838 | } |
| 7839 | |
| 7840 | connection_.SendStreamData3(); |
| 7841 | } |
| 7842 | |
| 7843 | // Verify that the connection does not become app-limited if there is |
| 7844 | // outstanding data to send after the write. |
| 7845 | TEST_P(QuicConnectionTest, NotBecomeApplicationLimitedIfMoreDataAvailable) { |
| 7846 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(0); |
| 7847 | { |
| 7848 | InSequence seq; |
| 7849 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 7850 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 7851 | } |
| 7852 | |
| 7853 | connection_.SendStreamData3(); |
| 7854 | } |
| 7855 | |
| 7856 | // Verify that the connection does not become app-limited after blocked write |
| 7857 | // even if there is outstanding data to send after the write. |
| 7858 | TEST_P(QuicConnectionTest, NotBecomeApplicationLimitedDueToWriteBlock) { |
| 7859 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(0); |
| 7860 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 7861 | BlockOnNextWrite(); |
| 7862 | |
| 7863 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7864 | connection_.SendStreamData3(); |
| 7865 | |
| 7866 | // Now unblock the writer, become congestion control blocked, |
| 7867 | // and ensure we become app-limited after writing. |
| 7868 | writer_->SetWritable(); |
| 7869 | CongestionBlockWrites(); |
| 7870 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(false)); |
| 7871 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 7872 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(1); |
| 7873 | connection_.OnCanWrite(); |
| 7874 | } |
| 7875 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7876 | TEST_P(QuicConnectionTest, DoNotForceSendingAckOnPacketTooLarge) { |
| 7877 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7878 | // Send an ack by simulating delayed ack alarm firing. |
| 7879 | ProcessPacket(1); |
| 7880 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 7881 | connection_.GetAckAlarm()->Fire(); |
| 7882 | // Simulate data packet causes write error. |
| 7883 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 7884 | SimulateNextPacketTooLarge(); |
| 7885 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 7886 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 7887 | // Ack frame is not bundled in connection close packet. |
| 7888 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 7889 | if (writer_->padding_frames().empty()) { |
| 7890 | EXPECT_EQ(1u, writer_->frame_count()); |
| 7891 | } else { |
| 7892 | EXPECT_EQ(2u, writer_->frame_count()); |
| 7893 | } |
| 7894 | |
| 7895 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 7896 | } |
| 7897 | |
| 7898 | TEST_P(QuicConnectionTest, CloseConnectionAllLevels) { |
| 7899 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7900 | return; |
| 7901 | } |
| 7902 | |
| 7903 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 7904 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 7905 | connection_.CloseConnection( |
| 7906 | kQuicErrorCode, "Some random error message", |
| 7907 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 7908 | |
| 7909 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 7910 | |
| 7911 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 7912 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 7913 | |
| 7914 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 7915 | // Each connection close packet should be sent in distinct UDP packets. |
| 7916 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 7917 | writer_->connection_close_packets()); |
| 7918 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 7919 | writer_->packets_write_attempts()); |
| 7920 | return; |
| 7921 | } |
| 7922 | |
| 7923 | // A single UDP packet should be sent with multiple connection close packets |
| 7924 | // coalesced together. |
| 7925 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7926 | |
| 7927 | // Only the first packet has been processed yet. |
| 7928 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 7929 | |
| 7930 | // ProcessPacket resets the visitor and frees the coalesced packet. |
| 7931 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 7932 | auto packet = writer_->coalesced_packet()->Clone(); |
| 7933 | writer_->framer()->ProcessPacket(*packet); |
| 7934 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 7935 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 7936 | } |
| 7937 | |
| 7938 | TEST_P(QuicConnectionTest, CloseConnectionOneLevel) { |
| 7939 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7940 | return; |
| 7941 | } |
| 7942 | |
| 7943 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 7944 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 7945 | connection_.CloseConnection( |
| 7946 | kQuicErrorCode, "Some random error message", |
| 7947 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 7948 | |
| 7949 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 7950 | |
| 7951 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 7952 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 7953 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 7954 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7955 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 7956 | } |
| 7957 | |
| 7958 | TEST_P(QuicConnectionTest, DoNotPadServerInitialConnectionClose) { |
| 7959 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7960 | return; |
| 7961 | } |
| 7962 | set_perspective(Perspective::IS_SERVER); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 7963 | // Receives packet 1000 in initial data. |
| 7964 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 7965 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 7966 | |
| 7967 | if (version().handshake_protocol == PROTOCOL_TLS1_3) { |
| 7968 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 7969 | } |
| 7970 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 7971 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 7972 | connection_.CloseConnection( |
| 7973 | kQuicErrorCode, "Some random error message", |
| 7974 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 7975 | |
| 7976 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 7977 | |
| 7978 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 7979 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 7980 | EXPECT_TRUE(writer_->padding_frames().empty()); |
| 7981 | EXPECT_EQ(ENCRYPTION_INITIAL, writer_->framer()->last_decrypted_level()); |
| 7982 | } |
| 7983 | |
| 7984 | // Regression test for b/63620844. |
| 7985 | TEST_P(QuicConnectionTest, FailedToWriteHandshakePacket) { |
| 7986 | SimulateNextPacketTooLarge(); |
| 7987 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7988 | .Times(1); |
| 7989 | |
| 7990 | connection_.SendCryptoStreamData(); |
| 7991 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 7992 | } |
| 7993 | |
| 7994 | TEST_P(QuicConnectionTest, MaxPacingRate) { |
| 7995 | EXPECT_EQ(0, connection_.MaxPacingRate().ToBytesPerSecond()); |
| 7996 | connection_.SetMaxPacingRate(QuicBandwidth::FromBytesPerSecond(100)); |
| 7997 | EXPECT_EQ(100, connection_.MaxPacingRate().ToBytesPerSecond()); |
| 7998 | } |
| 7999 | |
| 8000 | TEST_P(QuicConnectionTest, ClientAlwaysSendConnectionId) { |
| 8001 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 8002 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8003 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 8004 | EXPECT_EQ(CONNECTION_ID_PRESENT, |
| 8005 | writer_->last_packet_header().destination_connection_id_included); |
| 8006 | |
| 8007 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8008 | QuicConfig config; |
| 8009 | QuicConfigPeer::SetReceivedBytesForConnectionId(&config, 0); |
| 8010 | connection_.SetFromConfig(config); |
| 8011 | |
| 8012 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8013 | connection_.SendStreamDataWithString(3, "bar", 3, NO_FIN); |
| 8014 | // Verify connection id is still sent in the packet. |
| 8015 | EXPECT_EQ(CONNECTION_ID_PRESENT, |
| 8016 | writer_->last_packet_header().destination_connection_id_included); |
| 8017 | } |
| 8018 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8019 | TEST_P(QuicConnectionTest, PingAfterLastRetransmittablePacketAcked) { |
| 8020 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 8021 | QuicTime::Delta::FromMilliseconds(50); |
| 8022 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8023 | retransmittable_on_wire_timeout); |
| 8024 | |
| 8025 | EXPECT_TRUE(connection_.connected()); |
| 8026 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8027 | .WillRepeatedly(Return(true)); |
| 8028 | |
| 8029 | const char data[] = "data"; |
| 8030 | size_t data_size = strlen(data); |
| 8031 | QuicStreamOffset offset = 0; |
| 8032 | |
| 8033 | // Advance 5ms, send a retransmittable packet to the peer. |
| 8034 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8035 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8036 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8037 | offset += data_size; |
| 8038 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8039 | // The ping alarm is set for the ping timeout, not the shorter |
| 8040 | // retransmittable_on_wire_timeout. |
| 8041 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8042 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
| 8043 | EXPECT_EQ(ping_delay, |
| 8044 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8045 | |
| 8046 | // Advance 5ms, send a second retransmittable packet to the peer. |
| 8047 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8048 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8049 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8050 | offset += data_size; |
| 8051 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8052 | |
| 8053 | // Now receive an ACK of the first packet. This should not set the |
| 8054 | // retransmittable-on-wire alarm since packet 2 is still on the wire. |
| 8055 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8056 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8057 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8058 | QuicAckFrame frame = |
| 8059 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8060 | ProcessAckPacket(&frame); |
| 8061 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8062 | // The ping alarm is set for the ping timeout, not the shorter |
| 8063 | // retransmittable_on_wire_timeout. |
| 8064 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8065 | // The ping alarm has a 1 second granularity, and the clock has been advanced |
| 8066 | // 10ms since it was originally set. |
| 8067 | EXPECT_EQ(ping_delay - QuicTime::Delta::FromMilliseconds(10), |
| 8068 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8069 | |
| 8070 | // Now receive an ACK of the second packet. This should set the |
| 8071 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8072 | // the wire. |
| 8073 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8074 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8075 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8076 | ProcessAckPacket(&frame); |
| 8077 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8078 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8079 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8080 | |
| 8081 | // Now receive a duplicate ACK of the second packet. This should not update |
| 8082 | // the ping alarm. |
| 8083 | QuicTime prev_deadline = connection_.GetPingAlarm()->deadline(); |
| 8084 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8085 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8086 | ProcessAckPacket(&frame); |
| 8087 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8088 | EXPECT_EQ(prev_deadline, connection_.GetPingAlarm()->deadline()); |
| 8089 | |
| 8090 | // Now receive a non-ACK packet. This should not update the ping alarm. |
| 8091 | prev_deadline = connection_.GetPingAlarm()->deadline(); |
| 8092 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8093 | ProcessPacket(4); |
| 8094 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8095 | EXPECT_EQ(prev_deadline, connection_.GetPingAlarm()->deadline()); |
| 8096 | |
| 8097 | // Simulate the alarm firing and check that a PING is sent. |
| 8098 | connection_.GetPingAlarm()->Fire(); |
| 8099 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 8100 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8101 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 8102 | } |
| 8103 | |
| 8104 | TEST_P(QuicConnectionTest, NoPingIfRetransmittablePacketSent) { |
| 8105 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 8106 | QuicTime::Delta::FromMilliseconds(50); |
| 8107 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8108 | retransmittable_on_wire_timeout); |
| 8109 | |
| 8110 | EXPECT_TRUE(connection_.connected()); |
| 8111 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8112 | .WillRepeatedly(Return(true)); |
| 8113 | |
| 8114 | const char data[] = "data"; |
| 8115 | size_t data_size = strlen(data); |
| 8116 | QuicStreamOffset offset = 0; |
| 8117 | |
| 8118 | // Advance 5ms, send a retransmittable packet to the peer. |
| 8119 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8120 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8121 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8122 | offset += data_size; |
| 8123 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8124 | // The ping alarm is set for the ping timeout, not the shorter |
| 8125 | // retransmittable_on_wire_timeout. |
| 8126 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8127 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
| 8128 | EXPECT_EQ(ping_delay, |
| 8129 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8130 | |
| 8131 | // Now receive an ACK of the first packet. This should set the |
| 8132 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8133 | // the wire. |
| 8134 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8135 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8136 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8137 | QuicAckFrame frame = |
| 8138 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8139 | ProcessAckPacket(&frame); |
| 8140 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8141 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8142 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8143 | |
| 8144 | // Before the alarm fires, send another retransmittable packet. This should |
| 8145 | // cancel the retransmittable-on-wire alarm since now there's a |
| 8146 | // retransmittable packet on the wire. |
| 8147 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8148 | offset += data_size; |
| 8149 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8150 | |
| 8151 | // Now receive an ACK of the second packet. This should set the |
| 8152 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8153 | // the wire. |
| 8154 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8155 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8156 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8157 | ProcessAckPacket(&frame); |
| 8158 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8159 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8160 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8161 | |
| 8162 | // Simulate the alarm firing and check that a PING is sent. |
| 8163 | writer_->Reset(); |
| 8164 | connection_.GetPingAlarm()->Fire(); |
| 8165 | size_t padding_frame_count = writer_->padding_frames().size(); |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 8166 | // Do not ACK acks. |
| 8167 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8168 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 8169 | } |
| 8170 | |
| 8171 | // When there is no stream data received but are open streams, send the |
| 8172 | // first few consecutive pings with aggressive retransmittable-on-wire |
| 8173 | // timeout. Exponentially back off the retransmittable-on-wire ping timeout |
| 8174 | // afterwards until it exceeds the default ping timeout. |
| 8175 | TEST_P(QuicConnectionTest, BackOffRetransmittableOnWireTimeout) { |
| 8176 | int max_aggressive_retransmittable_on_wire_ping_count = 5; |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 8177 | SetQuicFlag(quic_max_aggressive_retransmittable_on_wire_ping_count, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8178 | max_aggressive_retransmittable_on_wire_ping_count); |
| 8179 | const QuicTime::Delta initial_retransmittable_on_wire_timeout = |
| 8180 | QuicTime::Delta::FromMilliseconds(200); |
| 8181 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8182 | initial_retransmittable_on_wire_timeout); |
| 8183 | |
| 8184 | EXPECT_TRUE(connection_.connected()); |
| 8185 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8186 | .WillRepeatedly(Return(true)); |
| 8187 | |
| 8188 | const char data[] = "data"; |
| 8189 | // Advance 5ms, send a retransmittable data packet to the peer. |
| 8190 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8191 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8192 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8193 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8194 | // The ping alarm is set for the ping timeout, not the shorter |
| 8195 | // retransmittable_on_wire_timeout. |
| 8196 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8197 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8198 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8199 | |
| 8200 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8201 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8202 | .Times(AnyNumber()); |
| 8203 | |
| 8204 | // Verify that the first few consecutive retransmittable on wire pings are |
| 8205 | // sent with aggressive timeout. |
| 8206 | for (int i = 0; i <= max_aggressive_retransmittable_on_wire_ping_count; i++) { |
| 8207 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8208 | // with the initial retransmittable-on-wire timeout. |
| 8209 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8210 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8211 | QuicAckFrame frame = InitAckFrame( |
| 8212 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8213 | ProcessAckPacket(&frame); |
| 8214 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8215 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8216 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8217 | // Simulate the alarm firing and check that a PING is sent. |
| 8218 | writer_->Reset(); |
| 8219 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8220 | connection_.GetPingAlarm()->Fire(); |
| 8221 | } |
| 8222 | |
| 8223 | QuicTime::Delta retransmittable_on_wire_timeout = |
| 8224 | initial_retransmittable_on_wire_timeout; |
| 8225 | |
| 8226 | // Verify subsequent pings are sent with timeout that is exponentially backed |
| 8227 | // off. |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8228 | while (retransmittable_on_wire_timeout * 2 < |
| 8229 | QuicTime::Delta::FromSeconds(kPingTimeoutSecs)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8230 | // Receive an ACK for the previous PING. This should set the |
| 8231 | // ping alarm with backed off retransmittable-on-wire timeout. |
| 8232 | retransmittable_on_wire_timeout = retransmittable_on_wire_timeout * 2; |
| 8233 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8234 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8235 | QuicAckFrame frame = InitAckFrame( |
| 8236 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8237 | ProcessAckPacket(&frame); |
| 8238 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8239 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8240 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8241 | |
| 8242 | // Simulate the alarm firing and check that a PING is sent. |
| 8243 | writer_->Reset(); |
| 8244 | clock_.AdvanceTime(retransmittable_on_wire_timeout); |
| 8245 | connection_.GetPingAlarm()->Fire(); |
| 8246 | } |
| 8247 | |
| 8248 | // The ping alarm is set with default ping timeout. |
| 8249 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8250 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8251 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8252 | |
| 8253 | // Receive an ACK for the previous PING. The ping alarm is set with an |
| 8254 | // earlier deadline. |
| 8255 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8256 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8257 | QuicAckFrame frame = InitAckFrame( |
| 8258 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8259 | ProcessAckPacket(&frame); |
| 8260 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8261 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs) - |
| 8262 | QuicTime::Delta::FromMilliseconds(5), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8263 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8264 | } |
| 8265 | |
| 8266 | // This test verify that the count of consecutive aggressive pings is reset |
| 8267 | // when new data is received. And it also verifies the connection resets |
| 8268 | // the exponential back-off of the retransmittable-on-wire ping timeout |
| 8269 | // after receiving new stream data. |
| 8270 | TEST_P(QuicConnectionTest, ResetBackOffRetransmitableOnWireTimeout) { |
| 8271 | int max_aggressive_retransmittable_on_wire_ping_count = 3; |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 8272 | SetQuicFlag(quic_max_aggressive_retransmittable_on_wire_ping_count, 3); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8273 | const QuicTime::Delta initial_retransmittable_on_wire_timeout = |
| 8274 | QuicTime::Delta::FromMilliseconds(200); |
| 8275 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8276 | initial_retransmittable_on_wire_timeout); |
| 8277 | |
| 8278 | EXPECT_TRUE(connection_.connected()); |
| 8279 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8280 | .WillRepeatedly(Return(true)); |
| 8281 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8282 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8283 | .Times(AnyNumber()); |
| 8284 | |
| 8285 | const char data[] = "data"; |
| 8286 | // Advance 5ms, send a retransmittable data packet to the peer. |
| 8287 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8288 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8289 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8290 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8291 | // The ping alarm is set for the ping timeout, not the shorter |
| 8292 | // retransmittable_on_wire_timeout. |
| 8293 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8294 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8295 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8296 | |
| 8297 | // Receive an ACK of the first packet. This should set the ping alarm with |
| 8298 | // initial retransmittable-on-wire timeout since there is no retransmittable |
| 8299 | // packet on the wire. |
| 8300 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8301 | QuicAckFrame frame = |
| 8302 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8303 | ProcessAckPacket(&frame); |
| 8304 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8305 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8306 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8307 | |
| 8308 | // Simulate the alarm firing and check that a PING is sent. |
| 8309 | writer_->Reset(); |
| 8310 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8311 | connection_.GetPingAlarm()->Fire(); |
| 8312 | |
| 8313 | // Receive an ACK for the previous PING. Ping alarm will be set with |
| 8314 | // aggressive timeout. |
| 8315 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8316 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8317 | frame = InitAckFrame( |
| 8318 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8319 | ProcessAckPacket(&frame); |
| 8320 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8321 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8322 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8323 | |
| 8324 | // Process a data packet. |
| 8325 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 8326 | ProcessDataPacket(peer_creator_.packet_number() + 1); |
| 8327 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, |
| 8328 | peer_creator_.packet_number() + 1); |
| 8329 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8330 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
fayang | 327663d | 2022-05-10 18:25:36 -0700 | [diff] [blame] | 8331 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8332 | connection_.GetPingAlarm()->Fire(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8333 | |
| 8334 | // Verify the count of consecutive aggressive pings is reset. |
| 8335 | for (int i = 0; i < max_aggressive_retransmittable_on_wire_ping_count; i++) { |
| 8336 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8337 | // with the initial retransmittable-on-wire timeout. |
| 8338 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8339 | QuicAckFrame frame = InitAckFrame( |
| 8340 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8341 | ProcessAckPacket(&frame); |
| 8342 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8343 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8344 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8345 | // Simulate the alarm firing and check that a PING is sent. |
| 8346 | writer_->Reset(); |
| 8347 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8348 | connection_.GetPingAlarm()->Fire(); |
| 8349 | // Advance 5ms to receive next packet. |
| 8350 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8351 | } |
| 8352 | |
| 8353 | // Receive another ACK for the previous PING. This should set the |
| 8354 | // ping alarm with backed off retransmittable-on-wire timeout. |
| 8355 | ack_num = creator_->packet_number(); |
| 8356 | frame = InitAckFrame( |
| 8357 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8358 | ProcessAckPacket(&frame); |
| 8359 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8360 | EXPECT_EQ(initial_retransmittable_on_wire_timeout * 2, |
| 8361 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8362 | |
| 8363 | writer_->Reset(); |
| 8364 | clock_.AdvanceTime(2 * initial_retransmittable_on_wire_timeout); |
| 8365 | connection_.GetPingAlarm()->Fire(); |
| 8366 | |
| 8367 | // Process another data packet and a new ACK packet. The ping alarm is set |
| 8368 | // with aggressive ping timeout again. |
| 8369 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 8370 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8371 | ProcessDataPacket(peer_creator_.packet_number() + 1); |
| 8372 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, |
| 8373 | peer_creator_.packet_number() + 1); |
| 8374 | ack_num = creator_->packet_number(); |
| 8375 | frame = InitAckFrame( |
| 8376 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8377 | ProcessAckPacket(&frame); |
| 8378 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8379 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8380 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8381 | } |
| 8382 | |
| 8383 | // Make sure that we never send more retransmissible on the wire pings than |
| 8384 | // the limit in FLAGS_quic_max_retransmittable_on_wire_ping_count. |
| 8385 | TEST_P(QuicConnectionTest, RetransmittableOnWirePingLimit) { |
| 8386 | static constexpr int kMaxRetransmittableOnWirePingCount = 3; |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 8387 | SetQuicFlag(quic_max_retransmittable_on_wire_ping_count, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8388 | kMaxRetransmittableOnWirePingCount); |
| 8389 | static constexpr QuicTime::Delta initial_retransmittable_on_wire_timeout = |
| 8390 | QuicTime::Delta::FromMilliseconds(200); |
| 8391 | static constexpr QuicTime::Delta short_delay = |
| 8392 | QuicTime::Delta::FromMilliseconds(5); |
| 8393 | ASSERT_LT(short_delay * 10, initial_retransmittable_on_wire_timeout); |
| 8394 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8395 | initial_retransmittable_on_wire_timeout); |
| 8396 | |
| 8397 | EXPECT_TRUE(connection_.connected()); |
| 8398 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8399 | .WillRepeatedly(Return(true)); |
| 8400 | |
| 8401 | const char data[] = "data"; |
| 8402 | // Advance 5ms, send a retransmittable data packet to the peer. |
| 8403 | clock_.AdvanceTime(short_delay); |
| 8404 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8405 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8406 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8407 | // The ping alarm is set for the ping timeout, not the shorter |
| 8408 | // retransmittable_on_wire_timeout. |
| 8409 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8410 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8411 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8412 | |
| 8413 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8414 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8415 | .Times(AnyNumber()); |
| 8416 | |
| 8417 | // Verify that the first few consecutive retransmittable on wire pings are |
| 8418 | // sent with aggressive timeout. |
| 8419 | for (int i = 0; i <= kMaxRetransmittableOnWirePingCount; i++) { |
| 8420 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8421 | // with the initial retransmittable-on-wire timeout. |
| 8422 | clock_.AdvanceTime(short_delay); |
| 8423 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8424 | QuicAckFrame frame = InitAckFrame( |
| 8425 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8426 | ProcessAckPacket(&frame); |
| 8427 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8428 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8429 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8430 | // Simulate the alarm firing and check that a PING is sent. |
| 8431 | writer_->Reset(); |
| 8432 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8433 | connection_.GetPingAlarm()->Fire(); |
| 8434 | } |
| 8435 | |
| 8436 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8437 | // but this time with the default ping timeout. |
| 8438 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8439 | QuicAckFrame frame = InitAckFrame( |
| 8440 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8441 | ProcessAckPacket(&frame); |
| 8442 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
fayang | 5d39333 | 2022-04-18 13:34:54 -0700 | [diff] [blame] | 8443 | EXPECT_EQ(QuicTime::Delta::FromSeconds(kPingTimeoutSecs), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8444 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8445 | } |
| 8446 | |
| 8447 | TEST_P(QuicConnectionTest, ValidStatelessResetToken) { |
| 8448 | const StatelessResetToken kTestToken{0, 1, 0, 1, 0, 1, 0, 1, |
| 8449 | 0, 1, 0, 1, 0, 1, 0, 1}; |
| 8450 | const StatelessResetToken kWrongTestToken{0, 1, 0, 1, 0, 1, 0, 1, |
| 8451 | 0, 1, 0, 1, 0, 1, 0, 2}; |
| 8452 | QuicConfig config; |
| 8453 | // No token has been received. |
| 8454 | EXPECT_FALSE(connection_.IsValidStatelessResetToken(kTestToken)); |
| 8455 | |
| 8456 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(2); |
| 8457 | // Token is different from received token. |
| 8458 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, kTestToken); |
| 8459 | connection_.SetFromConfig(config); |
| 8460 | EXPECT_FALSE(connection_.IsValidStatelessResetToken(kWrongTestToken)); |
| 8461 | |
| 8462 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, kTestToken); |
| 8463 | connection_.SetFromConfig(config); |
| 8464 | EXPECT_TRUE(connection_.IsValidStatelessResetToken(kTestToken)); |
| 8465 | } |
| 8466 | |
| 8467 | TEST_P(QuicConnectionTest, WriteBlockedWithInvalidAck) { |
| 8468 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8469 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 8470 | BlockOnNextWrite(); |
| 8471 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8472 | connection_.SendStreamDataWithString(5, "foo", 0, FIN); |
| 8473 | // This causes connection to be closed because packet 1 has not been sent yet. |
| 8474 | QuicAckFrame frame = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8475 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8476 | ProcessAckPacket(1, &frame); |
| 8477 | EXPECT_EQ(0, connection_close_frame_count_); |
| 8478 | } |
| 8479 | |
| 8480 | TEST_P(QuicConnectionTest, SendMessage) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8481 | if (connection_.version().UsesTls()) { |
| 8482 | QuicConfig config; |
| 8483 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 8484 | &config, kMaxAcceptedDatagramFrameSize); |
| 8485 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8486 | connection_.SetFromConfig(config); |
| 8487 | } |
| 8488 | std::string message(connection_.GetCurrentLargestMessagePayload() * 2, 'a'); |
| 8489 | quiche::QuicheMemSlice slice; |
| 8490 | { |
| 8491 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 8492 | connection_.SendStreamData3(); |
| 8493 | // Send a message which cannot fit into current open packet, and 2 packets |
| 8494 | // get sent, one contains stream frame, and the other only contains the |
| 8495 | // message frame. |
| 8496 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 8497 | slice = MemSliceFromString(absl::string_view( |
| 8498 | message.data(), connection_.GetCurrentLargestMessagePayload())); |
| 8499 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, |
| 8500 | connection_.SendMessage(1, absl::MakeSpan(&slice, 1), false)); |
| 8501 | } |
| 8502 | // Fail to send a message if connection is congestion control blocked. |
| 8503 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false)); |
| 8504 | slice = MemSliceFromString("message"); |
| 8505 | EXPECT_EQ(MESSAGE_STATUS_BLOCKED, |
| 8506 | connection_.SendMessage(2, absl::MakeSpan(&slice, 1), false)); |
| 8507 | |
| 8508 | // Always fail to send a message which cannot fit into one packet. |
| 8509 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8510 | slice = MemSliceFromString(absl::string_view( |
| 8511 | message.data(), connection_.GetCurrentLargestMessagePayload() + 1)); |
| 8512 | EXPECT_EQ(MESSAGE_STATUS_TOO_LARGE, |
| 8513 | connection_.SendMessage(3, absl::MakeSpan(&slice, 1), false)); |
| 8514 | } |
| 8515 | |
| 8516 | TEST_P(QuicConnectionTest, GetCurrentLargestMessagePayload) { |
martinduke | 225e91a | 2022-11-28 17:18:13 -0800 | [diff] [blame] | 8517 | QuicPacketLength expected_largest_payload = 1215; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8518 | if (connection_.version().SendsVariableLengthPacketNumberInLongHeader()) { |
| 8519 | expected_largest_payload += 3; |
| 8520 | } |
| 8521 | if (connection_.version().HasLongHeaderLengths()) { |
| 8522 | expected_largest_payload -= 2; |
| 8523 | } |
| 8524 | if (connection_.version().HasLengthPrefixedConnectionIds()) { |
| 8525 | expected_largest_payload -= 1; |
| 8526 | } |
| 8527 | if (connection_.version().UsesTls()) { |
| 8528 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 8529 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), 0); |
| 8530 | QuicConfig config; |
| 8531 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 8532 | &config, kMaxAcceptedDatagramFrameSize); |
| 8533 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8534 | connection_.SetFromConfig(config); |
| 8535 | // Verify the value post-handshake. |
| 8536 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), |
| 8537 | expected_largest_payload); |
| 8538 | } else { |
| 8539 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), |
| 8540 | expected_largest_payload); |
| 8541 | } |
| 8542 | } |
| 8543 | |
| 8544 | TEST_P(QuicConnectionTest, GetGuaranteedLargestMessagePayload) { |
martinduke | 225e91a | 2022-11-28 17:18:13 -0800 | [diff] [blame] | 8545 | QuicPacketLength expected_largest_payload = 1215; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8546 | if (connection_.version().HasLongHeaderLengths()) { |
| 8547 | expected_largest_payload -= 2; |
| 8548 | } |
| 8549 | if (connection_.version().HasLengthPrefixedConnectionIds()) { |
| 8550 | expected_largest_payload -= 1; |
| 8551 | } |
| 8552 | if (connection_.version().UsesTls()) { |
| 8553 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 8554 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), 0); |
| 8555 | QuicConfig config; |
| 8556 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 8557 | &config, kMaxAcceptedDatagramFrameSize); |
| 8558 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8559 | connection_.SetFromConfig(config); |
| 8560 | // Verify the value post-handshake. |
| 8561 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 8562 | expected_largest_payload); |
| 8563 | } else { |
| 8564 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 8565 | expected_largest_payload); |
| 8566 | } |
| 8567 | } |
| 8568 | |
| 8569 | TEST_P(QuicConnectionTest, LimitedLargestMessagePayload) { |
fayang | fc04b8a | 2023-05-18 09:26:25 -0700 | [diff] [blame^] | 8570 | if (!connection_.version().UsesTls()) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8571 | return; |
| 8572 | } |
| 8573 | constexpr QuicPacketLength kFrameSizeLimit = 1000; |
| 8574 | constexpr QuicPacketLength kPayloadSizeLimit = |
| 8575 | kFrameSizeLimit - kQuicFrameTypeSize; |
| 8576 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 8577 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), 0); |
| 8578 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), 0); |
| 8579 | QuicConfig config; |
| 8580 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize(&config, kFrameSizeLimit); |
| 8581 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8582 | connection_.SetFromConfig(config); |
| 8583 | // Verify the value post-handshake. |
| 8584 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), kPayloadSizeLimit); |
| 8585 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 8586 | kPayloadSizeLimit); |
| 8587 | } |
| 8588 | |
| 8589 | // Test to check that the path challenge/path response logic works |
| 8590 | // correctly. This test is only for version-99 |
| 8591 | TEST_P(QuicConnectionTest, ServerResponseToPathChallenge) { |
| 8592 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 8593 | return; |
| 8594 | } |
| 8595 | PathProbeTestInit(Perspective::IS_SERVER); |
| 8596 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 8597 | // First check if the server can send probing packet. |
| 8598 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 8599 | |
| 8600 | // Create and send the probe request (PATH_CHALLENGE frame). |
| 8601 | // SendConnectivityProbingPacket ends up calling |
| 8602 | // TestPacketWriter::WritePacket() which in turns receives and parses the |
| 8603 | // packet by calling framer_.ProcessPacket() -- which in turn calls |
| 8604 | // SimpleQuicFramer::OnPathChallengeFrame(). SimpleQuicFramer saves |
| 8605 | // the packet in writer_->path_challenge_frames() |
| 8606 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8607 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 8608 | connection_.peer_address()); |
| 8609 | // Save the random contents of the challenge for later comparison to the |
| 8610 | // response. |
| 8611 | ASSERT_GE(writer_->path_challenge_frames().size(), 1u); |
| 8612 | QuicPathFrameBuffer challenge_data = |
| 8613 | writer_->path_challenge_frames().front().data_buffer; |
| 8614 | |
| 8615 | // Normally, QuicConnection::OnPathChallengeFrame and OnPaddingFrame would be |
| 8616 | // called and it will perform actions to ensure that the rest of the protocol |
| 8617 | // is performed. |
| 8618 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8619 | EXPECT_TRUE(connection_.OnPathChallengeFrame( |
| 8620 | writer_->path_challenge_frames().front())); |
| 8621 | EXPECT_TRUE(connection_.OnPaddingFrame(writer_->padding_frames().front())); |
| 8622 | creator_->FlushCurrentPacket(); |
| 8623 | |
| 8624 | // The final check is to ensure that the random data in the response matches |
| 8625 | // the random data from the challenge. |
| 8626 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 8627 | EXPECT_EQ(0, memcmp(&challenge_data, |
| 8628 | &(writer_->path_response_frames().front().data_buffer), |
| 8629 | sizeof(challenge_data))); |
| 8630 | } |
| 8631 | |
| 8632 | TEST_P(QuicConnectionTest, ClientResponseToPathChallengeOnDefaulSocket) { |
| 8633 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 8634 | return; |
| 8635 | } |
| 8636 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 8637 | // First check if the client can send probing packet. |
| 8638 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 8639 | |
| 8640 | // Create and send the probe request (PATH_CHALLENGE frame). |
| 8641 | // SendConnectivityProbingPacket ends up calling |
| 8642 | // TestPacketWriter::WritePacket() which in turns receives and parses the |
| 8643 | // packet by calling framer_.ProcessPacket() -- which in turn calls |
| 8644 | // SimpleQuicFramer::OnPathChallengeFrame(). SimpleQuicFramer saves |
| 8645 | // the packet in writer_->path_challenge_frames() |
| 8646 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8647 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 8648 | connection_.peer_address()); |
| 8649 | // Save the random contents of the challenge for later validation against the |
| 8650 | // response. |
| 8651 | ASSERT_GE(writer_->path_challenge_frames().size(), 1u); |
| 8652 | QuicPathFrameBuffer challenge_data = |
| 8653 | writer_->path_challenge_frames().front().data_buffer; |
| 8654 | |
| 8655 | // Normally, QuicConnection::OnPathChallengeFrame would be |
| 8656 | // called and it will perform actions to ensure that the rest of the protocol |
| 8657 | // is performed. |
| 8658 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8659 | EXPECT_TRUE(connection_.OnPathChallengeFrame( |
| 8660 | writer_->path_challenge_frames().front())); |
| 8661 | EXPECT_TRUE(connection_.OnPaddingFrame(writer_->padding_frames().front())); |
| 8662 | creator_->FlushCurrentPacket(); |
| 8663 | |
| 8664 | // The final check is to ensure that the random data in the response matches |
| 8665 | // the random data from the challenge. |
| 8666 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 8667 | EXPECT_EQ(0, memcmp(&challenge_data, |
| 8668 | &(writer_->path_response_frames().front().data_buffer), |
| 8669 | sizeof(challenge_data))); |
| 8670 | } |
| 8671 | |
| 8672 | TEST_P(QuicConnectionTest, ClientResponseToPathChallengeOnAlternativeSocket) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 8673 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8674 | return; |
| 8675 | } |
| 8676 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 8677 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 8678 | |
| 8679 | QuicSocketAddress kNewSelfAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 8680 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 8681 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 8682 | .Times(AtLeast(1u)) |
| 8683 | .WillOnce(Invoke([&]() { |
| 8684 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 8685 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 8686 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 8687 | EXPECT_EQ(kNewSelfAddress.host(), |
| 8688 | new_writer.last_write_source_address()); |
| 8689 | })); |
| 8690 | bool success = false; |
| 8691 | connection_.ValidatePath( |
| 8692 | std::make_unique<TestQuicPathValidationContext>( |
| 8693 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 8694 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 8695 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 8696 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8697 | |
| 8698 | // Receiving a PATH_CHALLENGE on the alternative path. Response to this |
| 8699 | // PATH_CHALLENGE should be sent via the alternative writer. |
| 8700 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 8701 | .Times(AtLeast(1u)) |
| 8702 | .WillOnce(Invoke([&]() { |
| 8703 | EXPECT_EQ(2u, new_writer.packets_write_attempts()); |
| 8704 | EXPECT_EQ(1u, new_writer.path_response_frames().size()); |
| 8705 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 8706 | EXPECT_EQ(kNewSelfAddress.host(), |
| 8707 | new_writer.last_write_source_address()); |
| 8708 | })); |
| 8709 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 8710 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 8711 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 8712 | probing_packet->encrypted_length), |
| 8713 | clock_.Now())); |
| 8714 | ProcessReceivedPacket(kNewSelfAddress, kPeerAddress, *received); |
| 8715 | |
| 8716 | QuicSocketAddress kNewerSelfAddress(QuicIpAddress::Loopback6(), |
| 8717 | /*port=*/34567); |
| 8718 | // Receiving a PATH_CHALLENGE on an unknown socket should be ignored. |
| 8719 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0u); |
| 8720 | ProcessReceivedPacket(kNewerSelfAddress, kPeerAddress, *received); |
| 8721 | } |
| 8722 | |
| 8723 | TEST_P(QuicConnectionTest, |
| 8724 | RestartPathDegradingDetectionAfterMigrationWithProbe) { |
| 8725 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8726 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 8727 | |
| 8728 | // Send data and verify the path degrading detection is set. |
| 8729 | const char data[] = "data"; |
| 8730 | size_t data_size = strlen(data); |
| 8731 | QuicStreamOffset offset = 0; |
| 8732 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8733 | offset += data_size; |
| 8734 | |
| 8735 | // Verify the path degrading detection is in progress. |
| 8736 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 8737 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 8738 | QuicTime ddl = connection_.GetBlackholeDetectorAlarm()->deadline(); |
| 8739 | |
| 8740 | // Simulate the firing of path degrading. |
| 8741 | clock_.AdvanceTime(ddl - clock_.ApproximateNow()); |
| 8742 | EXPECT_CALL(visitor_, OnPathDegrading()).Times(1); |
| 8743 | connection_.PathDegradingTimeout(); |
| 8744 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8745 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 8746 | |
| 8747 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 8748 | // Simulate path degrading handling by sending a probe on an alternet path. |
| 8749 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8750 | TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 8751 | connection_.SendConnectivityProbingPacket(&probing_writer, |
| 8752 | connection_.peer_address()); |
| 8753 | // Verify that path degrading detection is not reset. |
| 8754 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
| 8755 | |
| 8756 | // Simulate successful path degrading handling by receiving probe response. |
| 8757 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 8758 | |
| 8759 | EXPECT_CALL(visitor_, |
| 8760 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 8761 | .Times(1); |
| 8762 | const QuicSocketAddress kNewSelfAddress = |
| 8763 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 8764 | |
| 8765 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 8766 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 8767 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 8768 | probing_packet->encrypted_length), |
| 8769 | clock_.Now())); |
| 8770 | uint64_t num_probing_received = |
| 8771 | connection_.GetStats().num_connectivity_probing_received; |
| 8772 | ProcessReceivedPacket(kNewSelfAddress, kPeerAddress, *received); |
| 8773 | |
| 8774 | EXPECT_EQ(num_probing_received + 1, |
| 8775 | connection_.GetStats().num_connectivity_probing_received); |
| 8776 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 8777 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 8778 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8779 | } |
| 8780 | |
| 8781 | // Verify new path degrading detection is activated. |
| 8782 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()).Times(1); |
| 8783 | connection_.OnSuccessfulMigration(/*is_port_change*/ true); |
| 8784 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 8785 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
| 8786 | } |
| 8787 | |
| 8788 | TEST_P(QuicConnectionTest, ClientsResetCwndAfterConnectionMigration) { |
| 8789 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 8790 | return; |
| 8791 | } |
| 8792 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8793 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 8794 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 8795 | |
| 8796 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 8797 | QuicTime::Delta default_init_rtt = rtt_stats->initial_rtt(); |
| 8798 | rtt_stats->set_initial_rtt(default_init_rtt * 2); |
| 8799 | EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt()); |
| 8800 | |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 8801 | QuicSentPacketManagerPeer::SetConsecutivePtoCount(manager_, 1); |
| 8802 | EXPECT_EQ(1u, manager_->GetConsecutivePtoCount()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8803 | const SendAlgorithmInterface* send_algorithm = manager_->GetSendAlgorithm(); |
| 8804 | |
| 8805 | // Migrate to a new address with different IP. |
| 8806 | const QuicSocketAddress kNewSelfAddress = |
| 8807 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 8808 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 8809 | connection_.MigratePath(kNewSelfAddress, connection_.peer_address(), |
| 8810 | &new_writer, false); |
| 8811 | EXPECT_EQ(default_init_rtt, manager_->GetRttStats()->initial_rtt()); |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 8812 | EXPECT_EQ(0u, manager_->GetConsecutivePtoCount()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8813 | EXPECT_NE(send_algorithm, manager_->GetSendAlgorithm()); |
| 8814 | } |
| 8815 | |
| 8816 | // Regression test for b/110259444 |
| 8817 | TEST_P(QuicConnectionTest, DoNotScheduleSpuriousAckAlarm) { |
| 8818 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8819 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 8820 | writer_->SetWriteBlocked(); |
| 8821 | |
| 8822 | ProcessPacket(1); |
| 8823 | // Verify ack alarm is set. |
| 8824 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 8825 | // Fire the ack alarm, verify no packet is sent because the writer is blocked. |
| 8826 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8827 | connection_.GetAckAlarm()->Fire(); |
| 8828 | |
| 8829 | writer_->SetWritable(); |
| 8830 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8831 | ProcessPacket(2); |
| 8832 | // Verify ack alarm is not set. |
| 8833 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 8834 | } |
| 8835 | |
| 8836 | TEST_P(QuicConnectionTest, DisablePacingOffloadConnectionOptions) { |
| 8837 | EXPECT_FALSE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 8838 | writer_->set_supports_release_time(true); |
| 8839 | QuicConfig config; |
| 8840 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8841 | connection_.SetFromConfig(config); |
| 8842 | EXPECT_TRUE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 8843 | |
| 8844 | QuicTagVector connection_options; |
| 8845 | connection_options.push_back(kNPCO); |
| 8846 | config.SetConnectionOptionsToSend(connection_options); |
| 8847 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8848 | connection_.SetFromConfig(config); |
| 8849 | // Verify pacing offload is disabled. |
| 8850 | EXPECT_FALSE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 8851 | } |
| 8852 | |
| 8853 | // Regression test for b/110259444 |
| 8854 | // Get a path response without having issued a path challenge... |
| 8855 | TEST_P(QuicConnectionTest, OrphanPathResponse) { |
| 8856 | QuicPathFrameBuffer data = {{0, 1, 2, 3, 4, 5, 6, 7}}; |
| 8857 | |
| 8858 | QuicPathResponseFrame frame(99, data); |
| 8859 | EXPECT_TRUE(connection_.OnPathResponseFrame(frame)); |
| 8860 | // If PATH_RESPONSE was accepted (payload matches the payload saved |
| 8861 | // in QuicConnection::transmitted_connectivity_probe_payload_) then |
| 8862 | // current_packet_content_ would be set to FIRST_FRAME_IS_PING. |
| 8863 | // Since this PATH_RESPONSE does not match, current_packet_content_ |
| 8864 | // must not be FIRST_FRAME_IS_PING. |
| 8865 | EXPECT_NE(QuicConnection::FIRST_FRAME_IS_PING, |
| 8866 | QuicConnectionPeer::GetCurrentPacketContent(&connection_)); |
| 8867 | } |
| 8868 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8869 | TEST_P(QuicConnectionTest, AcceptPacketNumberZero) { |
| 8870 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 8871 | return; |
| 8872 | } |
| 8873 | // Set first_sending_packet_number to be 0 to allow successfully processing |
| 8874 | // acks which ack packet number 0. |
| 8875 | QuicFramerPeer::SetFirstSendingPacketNumber(writer_->framer()->framer(), 0); |
| 8876 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8877 | |
| 8878 | ProcessPacket(0); |
| 8879 | EXPECT_EQ(QuicPacketNumber(0), LargestAcked(connection_.ack_frame())); |
| 8880 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 8881 | |
| 8882 | ProcessPacket(1); |
| 8883 | EXPECT_EQ(QuicPacketNumber(1), LargestAcked(connection_.ack_frame())); |
| 8884 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 8885 | |
| 8886 | ProcessPacket(2); |
| 8887 | EXPECT_EQ(QuicPacketNumber(2), LargestAcked(connection_.ack_frame())); |
| 8888 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
| 8889 | } |
| 8890 | |
| 8891 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacesBasicSending) { |
| 8892 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8893 | return; |
| 8894 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8895 | connection_.SendCryptoStreamData(); |
| 8896 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8897 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8898 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8899 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8900 | QuicAckFrame frame1 = InitAckFrame(1); |
| 8901 | // Received ACK for packet 1. |
| 8902 | ProcessFramePacketAtLevel(30, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 8903 | |
| 8904 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(4); |
| 8905 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 0, |
| 8906 | NO_FIN); |
| 8907 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 4, |
| 8908 | NO_FIN); |
| 8909 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 8910 | 8, NO_FIN); |
| 8911 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 8912 | 12, FIN); |
| 8913 | // Received ACK for packets 2, 4, 5. |
| 8914 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8915 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8916 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8917 | QuicAckFrame frame2 = |
| 8918 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}, |
| 8919 | {QuicPacketNumber(4), QuicPacketNumber(6)}}); |
| 8920 | // Make sure although the same packet number is used, but they are in |
| 8921 | // different packet number spaces. |
| 8922 | ProcessFramePacketAtLevel(30, QuicFrame(&frame2), ENCRYPTION_FORWARD_SECURE); |
| 8923 | } |
| 8924 | |
| 8925 | TEST_P(QuicConnectionTest, PeerAcksPacketsInWrongPacketNumberSpace) { |
| 8926 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8927 | return; |
| 8928 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8929 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 8930 | std::make_unique<TaggingEncrypter>(0x01)); |
| 8931 | |
| 8932 | connection_.SendCryptoStreamData(); |
| 8933 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8934 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 8935 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8936 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8937 | QuicAckFrame frame1 = InitAckFrame(1); |
| 8938 | // Received ACK for packet 1. |
| 8939 | ProcessFramePacketAtLevel(30, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 8940 | |
| 8941 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 8942 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 0, |
| 8943 | NO_FIN); |
| 8944 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 4, |
| 8945 | NO_FIN); |
| 8946 | |
| 8947 | // Received ACK for packets 2 and 3 in wrong packet number space. |
| 8948 | QuicAckFrame invalid_ack = |
| 8949 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(4)}}); |
| 8950 | EXPECT_CALL(visitor_, |
| 8951 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 8952 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 8953 | ProcessFramePacketAtLevel(300, QuicFrame(&invalid_ack), ENCRYPTION_INITIAL); |
| 8954 | TestConnectionCloseQuicErrorCode(QUIC_INVALID_ACK_DATA); |
| 8955 | } |
| 8956 | |
| 8957 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacesBasicReceiving) { |
| 8958 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8959 | return; |
| 8960 | } |
| 8961 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8962 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 8963 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 8964 | } |
| 8965 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8966 | // Receives packet 1000 in initial data. |
| 8967 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 8968 | EXPECT_TRUE(connection_.HasPendingAcks()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 8969 | peer_framer_.SetEncrypter( |
| 8970 | ENCRYPTION_FORWARD_SECURE, |
| 8971 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
| 8972 | SetDecrypter( |
| 8973 | ENCRYPTION_FORWARD_SECURE, |
| 8974 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8975 | // Receives packet 1000 in application data. |
| 8976 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_FORWARD_SECURE); |
| 8977 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 8978 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 8979 | 0, NO_FIN); |
| 8980 | // Verify application data ACK gets bundled with outgoing data. |
| 8981 | EXPECT_EQ(2u, writer_->frame_count()); |
| 8982 | // Make sure ACK alarm is still set because initial data is not ACKed. |
| 8983 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 8984 | // Receive packet 1001 in application data. |
| 8985 | ProcessDataPacketAtLevel(1001, false, ENCRYPTION_FORWARD_SECURE); |
| 8986 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 8987 | // Simulates ACK alarm fires and verify two ACKs are flushed. |
| 8988 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 8989 | connection_.SetEncrypter( |
| 8990 | ENCRYPTION_FORWARD_SECURE, |
| 8991 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 8992 | connection_.GetAckAlarm()->Fire(); |
| 8993 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 8994 | // Receives more packets in application data. |
| 8995 | ProcessDataPacketAtLevel(1002, false, ENCRYPTION_FORWARD_SECURE); |
| 8996 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 8997 | |
| 8998 | // Verify zero rtt and forward secure packets get acked in the same packet. |
| 8999 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9000 | ProcessDataPacket(1003); |
| 9001 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 9002 | } |
| 9003 | |
| 9004 | TEST_P(QuicConnectionTest, CancelAckAlarmOnWriteBlocked) { |
| 9005 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9006 | return; |
| 9007 | } |
| 9008 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9009 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9010 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9011 | } |
| 9012 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9013 | // Receives packet 1000 in initial data. |
| 9014 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 9015 | EXPECT_TRUE(connection_.HasPendingAcks()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 9016 | peer_framer_.SetEncrypter( |
| 9017 | ENCRYPTION_ZERO_RTT, |
| 9018 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9019 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 9020 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9021 | // Receives packet 1000 in application data. |
| 9022 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_ZERO_RTT); |
| 9023 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 9024 | |
| 9025 | writer_->SetWriteBlocked(); |
| 9026 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AnyNumber()); |
| 9027 | // Simulates ACK alarm fires and verify no ACK is flushed because of write |
| 9028 | // blocked. |
| 9029 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
| 9030 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9031 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 9032 | std::make_unique<TaggingEncrypter>(0x02)); |
| 9033 | connection_.GetAckAlarm()->Fire(); |
| 9034 | // Verify ACK alarm is not set. |
| 9035 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 9036 | |
| 9037 | writer_->SetWritable(); |
| 9038 | // Verify 2 ACKs are sent when connection gets unblocked. |
| 9039 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 9040 | connection_.OnCanWrite(); |
| 9041 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 9042 | } |
| 9043 | |
| 9044 | // Make sure a packet received with the right client connection ID is processed. |
| 9045 | TEST_P(QuicConnectionTest, ValidClientConnectionId) { |
| 9046 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9047 | return; |
| 9048 | } |
| 9049 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9050 | SetClientConnectionId(TestConnectionId(0x33)); |
| 9051 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_FORWARD_SECURE); |
| 9052 | header.destination_connection_id = TestConnectionId(0x33); |
| 9053 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 9054 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 9055 | QuicFrames frames; |
| 9056 | QuicPingFrame ping_frame; |
| 9057 | QuicPaddingFrame padding_frame; |
| 9058 | frames.push_back(QuicFrame(ping_frame)); |
| 9059 | frames.push_back(QuicFrame(padding_frame)); |
| 9060 | std::unique_ptr<QuicPacket> packet = |
| 9061 | BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 9062 | char buffer[kMaxOutgoingPacketSize]; |
| 9063 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9064 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 9065 | kMaxOutgoingPacketSize); |
| 9066 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9067 | false); |
| 9068 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9069 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9070 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9071 | } |
| 9072 | |
| 9073 | // Make sure a packet received with a different client connection ID is dropped. |
| 9074 | TEST_P(QuicConnectionTest, InvalidClientConnectionId) { |
| 9075 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9076 | return; |
| 9077 | } |
| 9078 | SetClientConnectionId(TestConnectionId(0x33)); |
| 9079 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_FORWARD_SECURE); |
| 9080 | header.destination_connection_id = TestConnectionId(0xbad); |
| 9081 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 9082 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 9083 | QuicFrames frames; |
| 9084 | QuicPingFrame ping_frame; |
| 9085 | QuicPaddingFrame padding_frame; |
| 9086 | frames.push_back(QuicFrame(ping_frame)); |
| 9087 | frames.push_back(QuicFrame(padding_frame)); |
| 9088 | std::unique_ptr<QuicPacket> packet = |
| 9089 | BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 9090 | char buffer[kMaxOutgoingPacketSize]; |
| 9091 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9092 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 9093 | kMaxOutgoingPacketSize); |
| 9094 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9095 | false); |
| 9096 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9097 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9098 | EXPECT_EQ(1u, connection_.GetStats().packets_dropped); |
| 9099 | } |
| 9100 | |
| 9101 | // Make sure the first packet received with a different client connection ID on |
| 9102 | // the server is processed and it changes the client connection ID. |
| 9103 | TEST_P(QuicConnectionTest, UpdateClientConnectionIdFromFirstPacket) { |
| 9104 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9105 | return; |
| 9106 | } |
| 9107 | set_perspective(Perspective::IS_SERVER); |
| 9108 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_INITIAL); |
| 9109 | header.source_connection_id = TestConnectionId(0x33); |
| 9110 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 9111 | QuicFrames frames; |
| 9112 | QuicPingFrame ping_frame; |
| 9113 | QuicPaddingFrame padding_frame; |
| 9114 | frames.push_back(QuicFrame(ping_frame)); |
| 9115 | frames.push_back(QuicFrame(padding_frame)); |
| 9116 | std::unique_ptr<QuicPacket> packet = |
| 9117 | BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 9118 | char buffer[kMaxOutgoingPacketSize]; |
| 9119 | size_t encrypted_length = |
| 9120 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(1), |
| 9121 | *packet, buffer, kMaxOutgoingPacketSize); |
| 9122 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9123 | false); |
| 9124 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9125 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9126 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9127 | EXPECT_EQ(TestConnectionId(0x33), connection_.client_connection_id()); |
| 9128 | } |
| 9129 | void QuicConnectionTest::TestReplaceConnectionIdFromInitial() { |
| 9130 | if (!framer_.version().AllowsVariableLengthConnectionIds()) { |
| 9131 | return; |
| 9132 | } |
| 9133 | // We start with a known connection ID. |
| 9134 | EXPECT_TRUE(connection_.connected()); |
| 9135 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9136 | EXPECT_NE(TestConnectionId(0x33), connection_.connection_id()); |
| 9137 | // Receiving an initial can replace the connection ID once. |
| 9138 | { |
| 9139 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_INITIAL); |
| 9140 | header.source_connection_id = TestConnectionId(0x33); |
| 9141 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 9142 | QuicFrames frames; |
| 9143 | QuicPingFrame ping_frame; |
| 9144 | QuicPaddingFrame padding_frame; |
| 9145 | frames.push_back(QuicFrame(ping_frame)); |
| 9146 | frames.push_back(QuicFrame(padding_frame)); |
| 9147 | std::unique_ptr<QuicPacket> packet = |
| 9148 | BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 9149 | char buffer[kMaxOutgoingPacketSize]; |
| 9150 | size_t encrypted_length = |
| 9151 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(1), |
| 9152 | *packet, buffer, kMaxOutgoingPacketSize); |
| 9153 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9154 | false); |
| 9155 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9156 | } |
| 9157 | EXPECT_TRUE(connection_.connected()); |
| 9158 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9159 | EXPECT_EQ(TestConnectionId(0x33), connection_.connection_id()); |
| 9160 | // Trying to replace the connection ID a second time drops the packet. |
| 9161 | { |
| 9162 | QuicPacketHeader header = ConstructPacketHeader(2, ENCRYPTION_INITIAL); |
| 9163 | header.source_connection_id = TestConnectionId(0x66); |
| 9164 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 9165 | QuicFrames frames; |
| 9166 | QuicPingFrame ping_frame; |
| 9167 | QuicPaddingFrame padding_frame; |
| 9168 | frames.push_back(QuicFrame(ping_frame)); |
| 9169 | frames.push_back(QuicFrame(padding_frame)); |
| 9170 | std::unique_ptr<QuicPacket> packet = |
| 9171 | BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 9172 | char buffer[kMaxOutgoingPacketSize]; |
| 9173 | size_t encrypted_length = |
| 9174 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(2), |
| 9175 | *packet, buffer, kMaxOutgoingPacketSize); |
| 9176 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9177 | false); |
| 9178 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9179 | } |
| 9180 | EXPECT_TRUE(connection_.connected()); |
| 9181 | EXPECT_EQ(1u, connection_.GetStats().packets_dropped); |
| 9182 | EXPECT_EQ(TestConnectionId(0x33), connection_.connection_id()); |
| 9183 | } |
| 9184 | |
| 9185 | TEST_P(QuicConnectionTest, ReplaceServerConnectionIdFromInitial) { |
| 9186 | TestReplaceConnectionIdFromInitial(); |
| 9187 | } |
| 9188 | |
| 9189 | TEST_P(QuicConnectionTest, ReplaceServerConnectionIdFromRetryAndInitial) { |
| 9190 | // First make the connection process a RETRY and replace the server connection |
| 9191 | // ID a first time. |
| 9192 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 9193 | /*missing_original_id_in_config=*/false, |
| 9194 | /*wrong_original_id_in_config=*/false, |
| 9195 | /*missing_retry_id_in_config=*/false, |
| 9196 | /*wrong_retry_id_in_config=*/false); |
| 9197 | // Reset the test framer to use the right connection ID. |
| 9198 | peer_framer_.SetInitialObfuscators(connection_.connection_id()); |
| 9199 | // Now process an INITIAL and replace the server connection ID a second time. |
| 9200 | TestReplaceConnectionIdFromInitial(); |
| 9201 | } |
| 9202 | |
| 9203 | // Regression test for b/134416344. |
| 9204 | TEST_P(QuicConnectionTest, CheckConnectedBeforeFlush) { |
| 9205 | // This test mimics a scenario where a connection processes 2 packets and the |
| 9206 | // 2nd packet contains connection close frame. When the 2nd flusher goes out |
| 9207 | // of scope, a delayed ACK is pending, and ACK alarm should not be scheduled |
| 9208 | // because connection is disconnected. |
| 9209 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9210 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 9211 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 9212 | const QuicErrorCode kErrorCode = QUIC_INTERNAL_ERROR; |
| 9213 | std::unique_ptr<QuicConnectionCloseFrame> connection_close_frame( |
| 9214 | new QuicConnectionCloseFrame(connection_.transport_version(), kErrorCode, |
| 9215 | NO_IETF_QUIC_ERROR, "", |
| 9216 | /*transport_close_frame_type=*/0)); |
| 9217 | |
| 9218 | // Received 2 packets. |
| 9219 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9220 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9221 | } else { |
| 9222 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 9223 | } |
| 9224 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 9225 | ENCRYPTION_INITIAL); |
| 9226 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 9227 | ProcessFramePacketWithAddresses(QuicFrame(connection_close_frame.release()), |
| 9228 | kSelfAddress, kPeerAddress, |
| 9229 | ENCRYPTION_INITIAL); |
| 9230 | // Verify ack alarm is not set. |
| 9231 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 9232 | } |
| 9233 | |
| 9234 | // Verify that a packet containing three coalesced packets is parsed correctly. |
| 9235 | TEST_P(QuicConnectionTest, CoalescedPacket) { |
| 9236 | if (!QuicVersionHasLongHeaderLengths(connection_.transport_version())) { |
| 9237 | // Coalesced packets can only be encoded using long header lengths. |
| 9238 | return; |
| 9239 | } |
| 9240 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9241 | EXPECT_TRUE(connection_.connected()); |
| 9242 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9243 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(3); |
| 9244 | } else { |
| 9245 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(3); |
| 9246 | } |
| 9247 | |
| 9248 | uint64_t packet_numbers[3] = {1, 2, 3}; |
| 9249 | EncryptionLevel encryption_levels[3] = { |
| 9250 | ENCRYPTION_INITIAL, ENCRYPTION_INITIAL, ENCRYPTION_FORWARD_SECURE}; |
| 9251 | char buffer[kMaxOutgoingPacketSize] = {}; |
| 9252 | size_t total_encrypted_length = 0; |
| 9253 | for (int i = 0; i < 3; i++) { |
| 9254 | QuicPacketHeader header = |
| 9255 | ConstructPacketHeader(packet_numbers[i], encryption_levels[i]); |
| 9256 | QuicFrames frames; |
| 9257 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9258 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 9259 | } else { |
| 9260 | frames.push_back(QuicFrame(frame1_)); |
| 9261 | } |
| 9262 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 9263 | peer_creator_.set_encryption_level(encryption_levels[i]); |
| 9264 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9265 | encryption_levels[i], QuicPacketNumber(packet_numbers[i]), *packet, |
| 9266 | buffer + total_encrypted_length, |
| 9267 | sizeof(buffer) - total_encrypted_length); |
| 9268 | EXPECT_GT(encrypted_length, 0u); |
| 9269 | total_encrypted_length += encrypted_length; |
| 9270 | } |
| 9271 | connection_.ProcessUdpPacket( |
| 9272 | kSelfAddress, kPeerAddress, |
| 9273 | QuicReceivedPacket(buffer, total_encrypted_length, clock_.Now(), false)); |
| 9274 | if (connection_.GetSendAlarm()->IsSet()) { |
| 9275 | connection_.GetSendAlarm()->Fire(); |
| 9276 | } |
| 9277 | |
| 9278 | EXPECT_TRUE(connection_.connected()); |
| 9279 | } |
| 9280 | |
| 9281 | // Regression test for crbug.com/992831. |
| 9282 | TEST_P(QuicConnectionTest, CoalescedPacketThatSavesFrames) { |
| 9283 | if (!QuicVersionHasLongHeaderLengths(connection_.transport_version())) { |
| 9284 | // Coalesced packets can only be encoded using long header lengths. |
| 9285 | return; |
| 9286 | } |
| 9287 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9288 | // TODO(b/129151114) Enable this test with multiple packet number spaces. |
| 9289 | return; |
| 9290 | } |
| 9291 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9292 | EXPECT_TRUE(connection_.connected()); |
| 9293 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9294 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 9295 | .Times(3) |
| 9296 | .WillRepeatedly([this](const QuicCryptoFrame& /*frame*/) { |
| 9297 | // QuicFrame takes ownership of the QuicBlockedFrame. |
QUICHE team | 1a27108 | 2022-05-18 10:22:22 -0700 | [diff] [blame] | 9298 | connection_.SendControlFrame(QuicFrame(QuicBlockedFrame(1, 3, 0))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9299 | }); |
| 9300 | } else { |
| 9301 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 9302 | .Times(3) |
| 9303 | .WillRepeatedly([this](const QuicStreamFrame& /*frame*/) { |
| 9304 | // QuicFrame takes ownership of the QuicBlockedFrame. |
QUICHE team | 1a27108 | 2022-05-18 10:22:22 -0700 | [diff] [blame] | 9305 | connection_.SendControlFrame(QuicFrame(QuicBlockedFrame(1, 3, 0))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9306 | }); |
| 9307 | } |
| 9308 | |
| 9309 | uint64_t packet_numbers[3] = {1, 2, 3}; |
| 9310 | EncryptionLevel encryption_levels[3] = { |
| 9311 | ENCRYPTION_INITIAL, ENCRYPTION_INITIAL, ENCRYPTION_FORWARD_SECURE}; |
| 9312 | char buffer[kMaxOutgoingPacketSize] = {}; |
| 9313 | size_t total_encrypted_length = 0; |
| 9314 | for (int i = 0; i < 3; i++) { |
| 9315 | QuicPacketHeader header = |
| 9316 | ConstructPacketHeader(packet_numbers[i], encryption_levels[i]); |
| 9317 | QuicFrames frames; |
| 9318 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9319 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 9320 | } else { |
| 9321 | frames.push_back(QuicFrame(frame1_)); |
| 9322 | } |
| 9323 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 9324 | peer_creator_.set_encryption_level(encryption_levels[i]); |
| 9325 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9326 | encryption_levels[i], QuicPacketNumber(packet_numbers[i]), *packet, |
| 9327 | buffer + total_encrypted_length, |
| 9328 | sizeof(buffer) - total_encrypted_length); |
| 9329 | EXPECT_GT(encrypted_length, 0u); |
| 9330 | total_encrypted_length += encrypted_length; |
| 9331 | } |
| 9332 | connection_.ProcessUdpPacket( |
| 9333 | kSelfAddress, kPeerAddress, |
| 9334 | QuicReceivedPacket(buffer, total_encrypted_length, clock_.Now(), false)); |
| 9335 | if (connection_.GetSendAlarm()->IsSet()) { |
| 9336 | connection_.GetSendAlarm()->Fire(); |
| 9337 | } |
| 9338 | |
| 9339 | EXPECT_TRUE(connection_.connected()); |
| 9340 | |
| 9341 | SendAckPacketToPeer(); |
| 9342 | } |
| 9343 | |
| 9344 | // Regresstion test for b/138962304. |
| 9345 | TEST_P(QuicConnectionTest, RtoAndWriteBlocked) { |
| 9346 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9347 | |
| 9348 | QuicStreamId stream_id = 2; |
| 9349 | QuicPacketNumber last_data_packet; |
| 9350 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 9351 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9352 | |
| 9353 | // Writer gets blocked. |
| 9354 | writer_->SetWriteBlocked(); |
| 9355 | |
| 9356 | // Cancel the stream. |
| 9357 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9358 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 9359 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 9360 | .WillRepeatedly( |
| 9361 | Invoke(¬ifier_, &SimpleSessionNotifier::WillingToWrite)); |
| 9362 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9363 | |
| 9364 | // Retransmission timer fires in RTO mode. |
| 9365 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9366 | // Verify no packets get flushed when writer is blocked. |
| 9367 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 9368 | } |
| 9369 | |
| 9370 | // Regresstion test for b/138962304. |
fayang | 339f0c8 | 2022-04-30 14:20:02 -0700 | [diff] [blame] | 9371 | TEST_P(QuicConnectionTest, PtoAndWriteBlocked) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9372 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9373 | |
| 9374 | QuicStreamId stream_id = 2; |
| 9375 | QuicPacketNumber last_data_packet; |
| 9376 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 9377 | SendStreamDataToPeer(4, "foo", 0, NO_FIN, &last_data_packet); |
| 9378 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9379 | |
| 9380 | // Writer gets blocked. |
| 9381 | writer_->SetWriteBlocked(); |
| 9382 | |
| 9383 | // Cancel stream 2. |
| 9384 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9385 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 9386 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9387 | |
| 9388 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9389 | // Retransmission timer fires in TLP mode. |
| 9390 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9391 | // Verify one packets is forced flushed when writer is blocked. |
| 9392 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 9393 | } |
| 9394 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9395 | TEST_P(QuicConnectionTest, ProbeTimeout) { |
| 9396 | QuicConfig config; |
| 9397 | QuicTagVector connection_options; |
| 9398 | connection_options.push_back(k2PTO); |
| 9399 | config.SetConnectionOptionsToSend(connection_options); |
| 9400 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9401 | connection_.SetFromConfig(config); |
| 9402 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9403 | |
| 9404 | QuicStreamId stream_id = 2; |
| 9405 | QuicPacketNumber last_packet; |
| 9406 | SendStreamDataToPeer(stream_id, "foooooo", 0, NO_FIN, &last_packet); |
| 9407 | SendStreamDataToPeer(stream_id, "foooooo", 7, NO_FIN, &last_packet); |
| 9408 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9409 | |
| 9410 | // Reset stream. |
| 9411 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9412 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9413 | |
| 9414 | // Fire the PTO and verify only the RST_STREAM is resent, not stream data. |
| 9415 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9416 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9417 | EXPECT_EQ(0u, writer_->stream_frames().size()); |
| 9418 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 9419 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9420 | } |
| 9421 | |
| 9422 | TEST_P(QuicConnectionTest, CloseConnectionAfter6ClientPTOs) { |
| 9423 | QuicConfig config; |
| 9424 | QuicTagVector connection_options; |
| 9425 | connection_options.push_back(k1PTO); |
| 9426 | connection_options.push_back(k6PTO); |
| 9427 | config.SetConnectionOptionsToSend(connection_options); |
| 9428 | QuicConfigPeer::SetNegotiated(&config, true); |
| 9429 | if (connection_.version().UsesTls()) { |
| 9430 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9431 | &config, connection_.connection_id()); |
| 9432 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9433 | &config, connection_.connection_id()); |
| 9434 | } |
| 9435 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9436 | connection_.SetFromConfig(config); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 9437 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2) || |
| 9438 | GetQuicReloadableFlag( |
| 9439 | quic_no_path_degrading_before_handshake_confirmed)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9440 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9441 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 9442 | } |
| 9443 | connection_.OnHandshakeComplete(); |
| 9444 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9445 | |
| 9446 | // Send stream data. |
| 9447 | SendStreamDataToPeer( |
| 9448 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 9449 | 0, FIN, nullptr); |
| 9450 | |
| 9451 | // Fire the retransmission alarm 5 times. |
| 9452 | for (int i = 0; i < 5; ++i) { |
| 9453 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9454 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9455 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9456 | EXPECT_TRUE(connection_.connected()); |
| 9457 | } |
| 9458 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 9459 | connection_.PathDegradingTimeout(); |
| 9460 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9461 | EXPECT_EQ(5u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 9462 | // Closes connection on 6th PTO. |
| 9463 | // May send multiple connecction close packets with multiple PN spaces. |
| 9464 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 9465 | EXPECT_CALL(visitor_, |
| 9466 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 9467 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 9468 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
| 9469 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9470 | EXPECT_FALSE(connection_.connected()); |
| 9471 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 9472 | } |
| 9473 | |
| 9474 | TEST_P(QuicConnectionTest, CloseConnectionAfter7ClientPTOs) { |
| 9475 | QuicConfig config; |
| 9476 | QuicTagVector connection_options; |
| 9477 | connection_options.push_back(k2PTO); |
| 9478 | connection_options.push_back(k7PTO); |
| 9479 | config.SetConnectionOptionsToSend(connection_options); |
| 9480 | QuicConfigPeer::SetNegotiated(&config, true); |
| 9481 | if (connection_.version().UsesTls()) { |
| 9482 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9483 | &config, connection_.connection_id()); |
| 9484 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9485 | &config, connection_.connection_id()); |
| 9486 | } |
| 9487 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9488 | connection_.SetFromConfig(config); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 9489 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2) || |
| 9490 | GetQuicReloadableFlag( |
| 9491 | quic_no_path_degrading_before_handshake_confirmed)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9492 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9493 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 9494 | } |
| 9495 | connection_.OnHandshakeComplete(); |
| 9496 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9497 | |
| 9498 | // Send stream data. |
| 9499 | SendStreamDataToPeer( |
| 9500 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 9501 | 0, FIN, nullptr); |
| 9502 | |
| 9503 | // Fire the retransmission alarm 6 times. |
| 9504 | for (int i = 0; i < 6; ++i) { |
| 9505 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 9506 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9507 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9508 | EXPECT_TRUE(connection_.connected()); |
| 9509 | } |
| 9510 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 9511 | connection_.PathDegradingTimeout(); |
| 9512 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9513 | EXPECT_EQ(6u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 9514 | // Closes connection on 7th PTO. |
| 9515 | EXPECT_CALL(visitor_, |
| 9516 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 9517 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 9518 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 9519 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
| 9520 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9521 | EXPECT_FALSE(connection_.connected()); |
| 9522 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 9523 | } |
| 9524 | |
| 9525 | TEST_P(QuicConnectionTest, CloseConnectionAfter8ClientPTOs) { |
| 9526 | QuicConfig config; |
| 9527 | QuicTagVector connection_options; |
| 9528 | connection_options.push_back(k2PTO); |
| 9529 | connection_options.push_back(k8PTO); |
| 9530 | QuicConfigPeer::SetNegotiated(&config, true); |
| 9531 | if (connection_.version().UsesTls()) { |
| 9532 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9533 | &config, connection_.connection_id()); |
| 9534 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9535 | &config, connection_.connection_id()); |
| 9536 | } |
| 9537 | config.SetConnectionOptionsToSend(connection_options); |
| 9538 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9539 | connection_.SetFromConfig(config); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 9540 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2) || |
| 9541 | GetQuicReloadableFlag( |
| 9542 | quic_no_path_degrading_before_handshake_confirmed)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9543 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9544 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 9545 | } |
| 9546 | connection_.OnHandshakeComplete(); |
| 9547 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9548 | |
| 9549 | // Send stream data. |
| 9550 | SendStreamDataToPeer( |
| 9551 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 9552 | 0, FIN, nullptr); |
| 9553 | |
| 9554 | // Fire the retransmission alarm 7 times. |
| 9555 | for (int i = 0; i < 7; ++i) { |
| 9556 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 9557 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9558 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9559 | EXPECT_TRUE(connection_.connected()); |
| 9560 | } |
| 9561 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 9562 | connection_.PathDegradingTimeout(); |
| 9563 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9564 | EXPECT_EQ(7u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 9565 | // Closes connection on 8th PTO. |
| 9566 | EXPECT_CALL(visitor_, |
| 9567 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 9568 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 9569 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 9570 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
| 9571 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9572 | EXPECT_FALSE(connection_.connected()); |
| 9573 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 9574 | } |
| 9575 | |
| 9576 | TEST_P(QuicConnectionTest, DeprecateHandshakeMode) { |
| 9577 | if (!connection_.version().SupportsAntiAmplificationLimit()) { |
| 9578 | return; |
| 9579 | } |
| 9580 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9581 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9582 | |
| 9583 | // Send CHLO. |
| 9584 | connection_.SendCryptoStreamData(); |
| 9585 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9586 | |
| 9587 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 9588 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9589 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9590 | QuicAckFrame frame1 = InitAckFrame(1); |
| 9591 | // Received ACK for packet 1. |
| 9592 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 9593 | |
| 9594 | // Verify retransmission alarm is still set because handshake is not |
| 9595 | // confirmed although there is nothing in flight. |
| 9596 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9597 | EXPECT_EQ(0u, connection_.GetStats().pto_count); |
| 9598 | EXPECT_EQ(0u, connection_.GetStats().crypto_retransmit_count); |
| 9599 | |
| 9600 | // PTO fires, verify a PING packet gets sent because there is no data to send. |
| 9601 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(3), _, _)); |
| 9602 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9603 | EXPECT_EQ(1u, connection_.GetStats().pto_count); |
| 9604 | EXPECT_EQ(1u, connection_.GetStats().crypto_retransmit_count); |
| 9605 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 9606 | } |
| 9607 | |
| 9608 | TEST_P(QuicConnectionTest, AntiAmplificationLimit) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 9609 | if (!connection_.version().SupportsAntiAmplificationLimit() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 9610 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9611 | return; |
| 9612 | } |
| 9613 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9614 | |
| 9615 | set_perspective(Perspective::IS_SERVER); |
| 9616 | // Verify no data can be sent at the beginning because bytes received is 0. |
| 9617 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9618 | connection_.SendCryptoDataWithString("foo", 0); |
| 9619 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 9620 | EXPECT_FALSE(connection_.CanWrite(NO_RETRANSMITTABLE_DATA)); |
| 9621 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9622 | |
| 9623 | // Receives packet 1. |
| 9624 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9625 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 9626 | |
| 9627 | const size_t anti_amplification_factor = |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 9628 | GetQuicFlag(quic_anti_amplification_factor); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9629 | // Verify now packets can be sent. |
| 9630 | for (size_t i = 1; i < anti_amplification_factor; ++i) { |
| 9631 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9632 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9633 | // Verify retransmission alarm is not set if throttled by anti-amplification |
| 9634 | // limit. |
| 9635 | EXPECT_EQ(i != anti_amplification_factor - 1, |
| 9636 | connection_.GetRetransmissionAlarm()->IsSet()); |
| 9637 | } |
| 9638 | // Verify server is throttled by anti-amplification limit. |
| 9639 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9640 | connection_.SendCryptoDataWithString("foo", anti_amplification_factor * 3); |
| 9641 | |
| 9642 | // Receives packet 2. |
| 9643 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9644 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 9645 | // Verify more packets can be sent. |
| 9646 | for (size_t i = anti_amplification_factor + 1; |
| 9647 | i < anti_amplification_factor * 2; ++i) { |
| 9648 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9649 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9650 | } |
| 9651 | // Verify server is throttled by anti-amplification limit. |
| 9652 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9653 | connection_.SendCryptoDataWithString("foo", |
| 9654 | 2 * anti_amplification_factor * 3); |
| 9655 | |
| 9656 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9657 | ProcessPacket(3); |
| 9658 | // Verify anti-amplification limit is gone after address validation. |
| 9659 | for (size_t i = 0; i < 100; ++i) { |
| 9660 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9661 | connection_.SendStreamDataWithString(3, "first", i * 0, NO_FIN); |
| 9662 | } |
| 9663 | } |
| 9664 | |
| 9665 | TEST_P(QuicConnectionTest, 3AntiAmplificationLimit) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 9666 | if (!connection_.version().SupportsAntiAmplificationLimit() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 9667 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9668 | return; |
| 9669 | } |
| 9670 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9671 | |
| 9672 | set_perspective(Perspective::IS_SERVER); |
| 9673 | QuicConfig config; |
| 9674 | QuicTagVector connection_options; |
| 9675 | connection_options.push_back(k3AFF); |
| 9676 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 9677 | if (connection_.version().UsesTls()) { |
| 9678 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9679 | &config, connection_.connection_id()); |
| 9680 | QuicConfigPeer::SetReceivedInitialSourceConnectionId(&config, |
| 9681 | QuicConnectionId()); |
| 9682 | } |
| 9683 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9684 | connection_.SetFromConfig(config); |
| 9685 | |
| 9686 | // Verify no data can be sent at the beginning because bytes received is 0. |
| 9687 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9688 | connection_.SendCryptoDataWithString("foo", 0); |
| 9689 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 9690 | EXPECT_FALSE(connection_.CanWrite(NO_RETRANSMITTABLE_DATA)); |
| 9691 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9692 | |
| 9693 | // Receives packet 1. |
| 9694 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9695 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 9696 | |
| 9697 | const size_t anti_amplification_factor = 3; |
| 9698 | // Verify now packets can be sent. |
| 9699 | for (size_t i = 1; i < anti_amplification_factor; ++i) { |
| 9700 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9701 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9702 | // Verify retransmission alarm is not set if throttled by anti-amplification |
| 9703 | // limit. |
| 9704 | EXPECT_EQ(i != anti_amplification_factor - 1, |
| 9705 | connection_.GetRetransmissionAlarm()->IsSet()); |
| 9706 | } |
| 9707 | // Verify server is throttled by anti-amplification limit. |
| 9708 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9709 | connection_.SendCryptoDataWithString("foo", anti_amplification_factor * 3); |
| 9710 | |
| 9711 | // Receives packet 2. |
| 9712 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9713 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 9714 | // Verify more packets can be sent. |
| 9715 | for (size_t i = anti_amplification_factor + 1; |
| 9716 | i < anti_amplification_factor * 2; ++i) { |
| 9717 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9718 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9719 | } |
| 9720 | // Verify server is throttled by anti-amplification limit. |
| 9721 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9722 | connection_.SendCryptoDataWithString("foo", |
| 9723 | 2 * anti_amplification_factor * 3); |
| 9724 | |
| 9725 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9726 | ProcessPacket(3); |
| 9727 | // Verify anti-amplification limit is gone after address validation. |
| 9728 | for (size_t i = 0; i < 100; ++i) { |
| 9729 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9730 | connection_.SendStreamDataWithString(3, "first", i * 0, NO_FIN); |
| 9731 | } |
| 9732 | } |
| 9733 | |
| 9734 | TEST_P(QuicConnectionTest, 10AntiAmplificationLimit) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 9735 | if (!connection_.version().SupportsAntiAmplificationLimit() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 9736 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9737 | return; |
| 9738 | } |
| 9739 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9740 | |
| 9741 | set_perspective(Perspective::IS_SERVER); |
| 9742 | QuicConfig config; |
| 9743 | QuicTagVector connection_options; |
| 9744 | connection_options.push_back(k10AF); |
| 9745 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 9746 | if (connection_.version().UsesTls()) { |
| 9747 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9748 | &config, connection_.connection_id()); |
| 9749 | QuicConfigPeer::SetReceivedInitialSourceConnectionId(&config, |
| 9750 | QuicConnectionId()); |
| 9751 | } |
| 9752 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9753 | connection_.SetFromConfig(config); |
| 9754 | |
| 9755 | // Verify no data can be sent at the beginning because bytes received is 0. |
| 9756 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9757 | connection_.SendCryptoDataWithString("foo", 0); |
| 9758 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 9759 | EXPECT_FALSE(connection_.CanWrite(NO_RETRANSMITTABLE_DATA)); |
| 9760 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9761 | |
| 9762 | // Receives packet 1. |
| 9763 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9764 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 9765 | |
| 9766 | const size_t anti_amplification_factor = 10; |
| 9767 | // Verify now packets can be sent. |
| 9768 | for (size_t i = 1; i < anti_amplification_factor; ++i) { |
| 9769 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9770 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9771 | // Verify retransmission alarm is not set if throttled by anti-amplification |
| 9772 | // limit. |
| 9773 | EXPECT_EQ(i != anti_amplification_factor - 1, |
| 9774 | connection_.GetRetransmissionAlarm()->IsSet()); |
| 9775 | } |
| 9776 | // Verify server is throttled by anti-amplification limit. |
| 9777 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9778 | connection_.SendCryptoDataWithString("foo", anti_amplification_factor * 3); |
| 9779 | |
| 9780 | // Receives packet 2. |
| 9781 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9782 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 9783 | // Verify more packets can be sent. |
| 9784 | for (size_t i = anti_amplification_factor + 1; |
| 9785 | i < anti_amplification_factor * 2; ++i) { |
| 9786 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9787 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 9788 | } |
| 9789 | // Verify server is throttled by anti-amplification limit. |
| 9790 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9791 | connection_.SendCryptoDataWithString("foo", |
| 9792 | 2 * anti_amplification_factor * 3); |
| 9793 | |
| 9794 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9795 | ProcessPacket(3); |
| 9796 | // Verify anti-amplification limit is gone after address validation. |
| 9797 | for (size_t i = 0; i < 100; ++i) { |
| 9798 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9799 | connection_.SendStreamDataWithString(3, "first", i * 0, NO_FIN); |
| 9800 | } |
| 9801 | } |
| 9802 | |
| 9803 | TEST_P(QuicConnectionTest, AckPendingWithAmplificationLimited) { |
| 9804 | if (!connection_.version().SupportsAntiAmplificationLimit()) { |
| 9805 | return; |
| 9806 | } |
| 9807 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9808 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(AnyNumber()); |
| 9809 | set_perspective(Perspective::IS_SERVER); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9810 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 9811 | // Receives packet 1. |
| 9812 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 9813 | connection_.SetEncrypter( |
| 9814 | ENCRYPTION_HANDSHAKE, |
| 9815 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9816 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 9817 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 9818 | // Send response in different encryption level and cause amplification factor |
| 9819 | // throttled. |
| 9820 | size_t i = 0; |
| 9821 | while (connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 9822 | connection_.SendCryptoDataWithString(std::string(1024, 'a'), i * 1024, |
| 9823 | ENCRYPTION_HANDSHAKE); |
| 9824 | ++i; |
| 9825 | } |
| 9826 | // Verify ACK is still pending. |
| 9827 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 9828 | |
| 9829 | // Fire ACK alarm and verify ACK cannot be sent due to amplification factor. |
| 9830 | clock_.AdvanceTime(connection_.GetAckAlarm()->deadline() - clock_.Now()); |
| 9831 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9832 | connection_.GetAckAlarm()->Fire(); |
| 9833 | // Verify ACK alarm is cancelled. |
| 9834 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 9835 | |
| 9836 | // Receives packet 2 and verify ACK gets flushed. |
| 9837 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9838 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 9839 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 9840 | } |
| 9841 | |
| 9842 | TEST_P(QuicConnectionTest, ConnectionCloseFrameType) { |
| 9843 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 9844 | // Test relevent only for IETF QUIC. |
| 9845 | return; |
| 9846 | } |
| 9847 | const QuicErrorCode kQuicErrorCode = IETF_QUIC_PROTOCOL_VIOLATION; |
| 9848 | // Use the (unknown) frame type of 9999 to avoid triggering any logic |
| 9849 | // which might be associated with the processing of a known frame type. |
| 9850 | const uint64_t kTransportCloseFrameType = 9999u; |
| 9851 | QuicFramerPeer::set_current_received_frame_type( |
| 9852 | QuicConnectionPeer::GetFramer(&connection_), kTransportCloseFrameType); |
| 9853 | // Do a transport connection close |
| 9854 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 9855 | connection_.CloseConnection( |
| 9856 | kQuicErrorCode, "Some random error message", |
| 9857 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 9858 | const std::vector<QuicConnectionCloseFrame>& connection_close_frames = |
| 9859 | writer_->connection_close_frames(); |
| 9860 | ASSERT_EQ(1u, connection_close_frames.size()); |
| 9861 | EXPECT_EQ(IETF_QUIC_TRANSPORT_CONNECTION_CLOSE, |
| 9862 | connection_close_frames[0].close_type); |
| 9863 | EXPECT_EQ(kQuicErrorCode, connection_close_frames[0].quic_error_code); |
| 9864 | EXPECT_EQ(kTransportCloseFrameType, |
| 9865 | connection_close_frames[0].transport_close_frame_type); |
| 9866 | } |
| 9867 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9868 | TEST_P(QuicConnectionTest, PtoSkipsPacketNumber) { |
| 9869 | QuicConfig config; |
| 9870 | QuicTagVector connection_options; |
| 9871 | connection_options.push_back(k1PTO); |
| 9872 | connection_options.push_back(kPTOS); |
| 9873 | config.SetConnectionOptionsToSend(connection_options); |
| 9874 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9875 | connection_.SetFromConfig(config); |
| 9876 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9877 | |
| 9878 | QuicStreamId stream_id = 2; |
| 9879 | QuicPacketNumber last_packet; |
| 9880 | SendStreamDataToPeer(stream_id, "foooooo", 0, NO_FIN, &last_packet); |
| 9881 | SendStreamDataToPeer(stream_id, "foooooo", 7, NO_FIN, &last_packet); |
| 9882 | EXPECT_EQ(QuicPacketNumber(2), last_packet); |
| 9883 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9884 | |
| 9885 | // Fire PTO and verify the PTO retransmission skips one packet number. |
| 9886 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9887 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9888 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 9889 | EXPECT_EQ(QuicPacketNumber(4), writer_->last_packet_header().packet_number); |
| 9890 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9891 | } |
| 9892 | |
| 9893 | TEST_P(QuicConnectionTest, SendCoalescedPackets) { |
| 9894 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 9895 | return; |
| 9896 | } |
| 9897 | MockQuicConnectionDebugVisitor debug_visitor; |
| 9898 | connection_.set_debug_visitor(&debug_visitor); |
| 9899 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)).Times(3); |
| 9900 | EXPECT_CALL(debug_visitor, OnCoalescedPacketSent(_, _)).Times(1); |
| 9901 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 9902 | { |
| 9903 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9904 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 9905 | connection_.SendCryptoDataWithString("foo", 0); |
| 9906 | // Verify this packet is on hold. |
| 9907 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 9908 | |
| 9909 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 9910 | std::make_unique<TaggingEncrypter>(0x02)); |
| 9911 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 9912 | connection_.SendCryptoDataWithString("bar", 3); |
| 9913 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 9914 | |
| 9915 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 9916 | std::make_unique<TaggingEncrypter>(0x03)); |
| 9917 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 9918 | SendStreamDataToPeer(2, "baz", 3, NO_FIN, nullptr); |
| 9919 | } |
| 9920 | // Verify all 3 packets are coalesced in the same UDP datagram. |
| 9921 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 9922 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
| 9923 | // Verify the packet is padded to full. |
| 9924 | EXPECT_EQ(connection_.max_packet_length(), writer_->last_packet_size()); |
| 9925 | |
| 9926 | // Verify packet process. |
| 9927 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 9928 | EXPECT_EQ(0u, writer_->stream_frames().size()); |
| 9929 | // Verify there is coalesced packet. |
| 9930 | EXPECT_NE(nullptr, writer_->coalesced_packet()); |
| 9931 | } |
| 9932 | |
| 9933 | TEST_P(QuicConnectionTest, FailToCoalescePacket) { |
| 9934 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 9935 | if (!IsDefaultTestConfiguration() || |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 9936 | !connection_.version().CanSendCoalescedPackets() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 9937 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9938 | return; |
| 9939 | } |
| 9940 | |
| 9941 | set_perspective(Perspective::IS_SERVER); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9942 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9943 | auto test_body = [&] { |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9944 | EXPECT_CALL(visitor_, |
| 9945 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 9946 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9947 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9948 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9949 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9950 | { |
| 9951 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9952 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 9953 | connection_.SendCryptoDataWithString("foo", 0); |
| 9954 | // Verify this packet is on hold. |
| 9955 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9956 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9957 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 9958 | std::make_unique<TaggingEncrypter>(0x02)); |
| 9959 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 9960 | connection_.SendCryptoDataWithString("bar", 3); |
| 9961 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9962 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9963 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 9964 | std::make_unique<TaggingEncrypter>(0x03)); |
| 9965 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 9966 | SendStreamDataToPeer(2, "baz", 3, NO_FIN, nullptr); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9967 | |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 9968 | creator_->Flush(); |
| 9969 | |
| 9970 | auto& coalesced_packet = |
| 9971 | QuicConnectionPeer::GetCoalescedPacket(&connection_); |
| 9972 | QuicPacketLength coalesced_packet_max_length = |
| 9973 | coalesced_packet.max_packet_length(); |
| 9974 | QuicCoalescedPacketPeer::SetMaxPacketLength(coalesced_packet, |
| 9975 | coalesced_packet.length()); |
| 9976 | |
| 9977 | // Make the coalescer's FORWARD_SECURE packet longer. |
| 9978 | *QuicCoalescedPacketPeer::GetMutableEncryptedBuffer( |
| 9979 | coalesced_packet, ENCRYPTION_FORWARD_SECURE) += "!!! TEST !!!"; |
| 9980 | |
| 9981 | QUIC_LOG(INFO) << "Reduced coalesced_packet_max_length from " |
| 9982 | << coalesced_packet_max_length << " to " |
| 9983 | << coalesced_packet.max_packet_length() |
| 9984 | << ", coalesced_packet.length:" |
| 9985 | << coalesced_packet.length() |
| 9986 | << ", coalesced_packet.packet_lengths:" |
| 9987 | << absl::StrJoin(coalesced_packet.packet_lengths(), ":"); |
| 9988 | } |
| 9989 | |
| 9990 | EXPECT_FALSE(connection_.connected()); |
| 9991 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 9992 | IsError(QUIC_FAILED_TO_SERIALIZE_PACKET)); |
| 9993 | EXPECT_EQ(saved_connection_close_frame_.error_details, |
| 9994 | "Failed to serialize coalesced packet."); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9995 | }; |
| 9996 | |
| 9997 | EXPECT_QUIC_BUG(test_body(), "SerializeCoalescedPacket failed."); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 9998 | } |
| 9999 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10000 | TEST_P(QuicConnectionTest, ClientReceivedHandshakeDone) { |
| 10001 | if (!connection_.version().UsesTls()) { |
| 10002 | return; |
| 10003 | } |
| 10004 | EXPECT_CALL(visitor_, OnHandshakeDoneReceived()); |
| 10005 | QuicFrames frames; |
| 10006 | frames.push_back(QuicFrame(QuicHandshakeDoneFrame())); |
| 10007 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10008 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10009 | } |
| 10010 | |
| 10011 | TEST_P(QuicConnectionTest, ServerReceivedHandshakeDone) { |
| 10012 | if (!connection_.version().UsesTls()) { |
| 10013 | return; |
| 10014 | } |
| 10015 | set_perspective(Perspective::IS_SERVER); |
| 10016 | EXPECT_CALL(visitor_, OnHandshakeDoneReceived()).Times(0); |
| 10017 | if (version().handshake_protocol == PROTOCOL_TLS1_3) { |
| 10018 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 10019 | } |
| 10020 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10021 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 10022 | QuicFrames frames; |
| 10023 | frames.push_back(QuicFrame(QuicHandshakeDoneFrame())); |
| 10024 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10025 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10026 | EXPECT_EQ(1, connection_close_frame_count_); |
| 10027 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 10028 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 10029 | } |
| 10030 | |
| 10031 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacePto) { |
| 10032 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10033 | return; |
| 10034 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10035 | // Send handshake packet. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10036 | connection_.SetEncrypter( |
| 10037 | ENCRYPTION_HANDSHAKE, |
| 10038 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10039 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10040 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10041 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10042 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10043 | |
| 10044 | // Send application data. |
| 10045 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 10046 | 0, NO_FIN); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10047 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10048 | QuicTime retransmission_time = |
| 10049 | connection_.GetRetransmissionAlarm()->deadline(); |
| 10050 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10051 | |
| 10052 | // Retransmit handshake data. |
| 10053 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 10054 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(4), _, _)); |
| 10055 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10056 | // Verify 1-RTT packet gets coalesced with handshake retransmission. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10057 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10058 | |
| 10059 | // Send application data. |
| 10060 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 10061 | 4, NO_FIN); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10062 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10063 | retransmission_time = connection_.GetRetransmissionAlarm()->deadline(); |
| 10064 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10065 | |
| 10066 | // Retransmit handshake data again. |
| 10067 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 10068 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(9), _, _)); |
| 10069 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(8), _, _)); |
| 10070 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10071 | // Verify 1-RTT packet gets coalesced with handshake retransmission. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10072 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10073 | |
| 10074 | // Discard handshake key. |
| 10075 | connection_.OnHandshakeComplete(); |
| 10076 | retransmission_time = connection_.GetRetransmissionAlarm()->deadline(); |
| 10077 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10078 | |
| 10079 | // Retransmit application data. |
| 10080 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 10081 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(11), _, _)); |
| 10082 | connection_.GetRetransmissionAlarm()->Fire(); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10083 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10084 | } |
| 10085 | |
| 10086 | void QuicConnectionTest::TestClientRetryHandling( |
| 10087 | bool invalid_retry_tag, bool missing_original_id_in_config, |
| 10088 | bool wrong_original_id_in_config, bool missing_retry_id_in_config, |
| 10089 | bool wrong_retry_id_in_config) { |
| 10090 | if (invalid_retry_tag) { |
| 10091 | ASSERT_FALSE(missing_original_id_in_config); |
| 10092 | ASSERT_FALSE(wrong_original_id_in_config); |
| 10093 | ASSERT_FALSE(missing_retry_id_in_config); |
| 10094 | ASSERT_FALSE(wrong_retry_id_in_config); |
| 10095 | } else { |
| 10096 | ASSERT_FALSE(missing_original_id_in_config && wrong_original_id_in_config); |
| 10097 | ASSERT_FALSE(missing_retry_id_in_config && wrong_retry_id_in_config); |
| 10098 | } |
| 10099 | if (!version().UsesTls()) { |
| 10100 | return; |
| 10101 | } |
| 10102 | |
| 10103 | // These values come from draft-ietf-quic-v2 Appendix A.4. |
| 10104 | uint8_t retry_packet_rfcv2[] = { |
martinduke | a2cb68a | 2022-12-12 17:24:39 -0800 | [diff] [blame] | 10105 | 0xcf, 0x6b, 0x33, 0x43, 0xcf, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10106 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0xc8, 0x64, 0x6c, 0xe8, |
| 10107 | 0xbf, 0xe3, 0x39, 0x52, 0xd9, 0x55, 0x54, 0x36, 0x65, 0xdc, 0xc7, 0xb6}; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10108 | // These values come from RFC9001 Appendix A.4. |
| 10109 | uint8_t retry_packet_rfcv1[] = { |
| 10110 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10111 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x04, 0xa2, 0x65, 0xba, |
| 10112 | 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f, 0x24, 0x96, 0xba}; |
| 10113 | uint8_t retry_packet29[] = { |
| 10114 | 0xff, 0xff, 0x00, 0x00, 0x1d, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10115 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0xd1, 0x69, 0x26, 0xd8, |
| 10116 | 0x1f, 0x6f, 0x9c, 0xa2, 0x95, 0x3a, 0x8a, 0xa4, 0x57, 0x5e, 0x1e, 0x49}; |
| 10117 | |
| 10118 | uint8_t* retry_packet; |
| 10119 | size_t retry_packet_length; |
martinduke | a2cb68a | 2022-12-12 17:24:39 -0800 | [diff] [blame] | 10120 | if (version() == ParsedQuicVersion::V2Draft08()) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10121 | retry_packet = retry_packet_rfcv2; |
| 10122 | retry_packet_length = ABSL_ARRAYSIZE(retry_packet_rfcv2); |
| 10123 | } else if (version() == ParsedQuicVersion::RFCv1()) { |
| 10124 | retry_packet = retry_packet_rfcv1; |
| 10125 | retry_packet_length = ABSL_ARRAYSIZE(retry_packet_rfcv1); |
| 10126 | } else if (version() == ParsedQuicVersion::Draft29()) { |
| 10127 | retry_packet = retry_packet29; |
| 10128 | retry_packet_length = ABSL_ARRAYSIZE(retry_packet29); |
| 10129 | } else { |
| 10130 | // TODO(dschinazi) generate retry packets for all versions once we have |
| 10131 | // server-side support for generating these programmatically. |
| 10132 | return; |
| 10133 | } |
| 10134 | |
| 10135 | uint8_t original_connection_id_bytes[] = {0x83, 0x94, 0xc8, 0xf0, |
| 10136 | 0x3e, 0x51, 0x57, 0x08}; |
| 10137 | uint8_t new_connection_id_bytes[] = {0xf0, 0x67, 0xa5, 0x50, |
| 10138 | 0x2a, 0x42, 0x62, 0xb5}; |
| 10139 | uint8_t retry_token_bytes[] = {0x74, 0x6f, 0x6b, 0x65, 0x6e}; |
| 10140 | |
| 10141 | QuicConnectionId original_connection_id( |
| 10142 | reinterpret_cast<char*>(original_connection_id_bytes), |
| 10143 | ABSL_ARRAYSIZE(original_connection_id_bytes)); |
| 10144 | QuicConnectionId new_connection_id( |
| 10145 | reinterpret_cast<char*>(new_connection_id_bytes), |
| 10146 | ABSL_ARRAYSIZE(new_connection_id_bytes)); |
| 10147 | |
| 10148 | std::string retry_token(reinterpret_cast<char*>(retry_token_bytes), |
| 10149 | ABSL_ARRAYSIZE(retry_token_bytes)); |
| 10150 | |
| 10151 | if (invalid_retry_tag) { |
| 10152 | // Flip the last bit of the retry packet to prevent the integrity tag |
| 10153 | // from validating correctly. |
| 10154 | retry_packet[retry_packet_length - 1] ^= 1; |
| 10155 | } |
| 10156 | |
| 10157 | QuicConnectionId config_original_connection_id = original_connection_id; |
| 10158 | if (wrong_original_id_in_config) { |
| 10159 | // Flip the first bit of the connection ID. |
| 10160 | ASSERT_FALSE(config_original_connection_id.IsEmpty()); |
| 10161 | config_original_connection_id.mutable_data()[0] ^= 0x80; |
| 10162 | } |
| 10163 | QuicConnectionId config_retry_source_connection_id = new_connection_id; |
| 10164 | if (wrong_retry_id_in_config) { |
| 10165 | // Flip the first bit of the connection ID. |
| 10166 | ASSERT_FALSE(config_retry_source_connection_id.IsEmpty()); |
| 10167 | config_retry_source_connection_id.mutable_data()[0] ^= 0x80; |
| 10168 | } |
| 10169 | |
| 10170 | // Make sure the connection uses the connection ID from the test vectors, |
| 10171 | QuicConnectionPeer::SetServerConnectionId(&connection_, |
| 10172 | original_connection_id); |
| 10173 | // Make sure our fake framer has the new post-retry INITIAL keys so that any |
| 10174 | // retransmission triggered by retry can be decrypted. |
| 10175 | writer_->framer()->framer()->SetInitialObfuscators(new_connection_id); |
| 10176 | |
| 10177 | // Process the RETRY packet. |
| 10178 | connection_.ProcessUdpPacket( |
| 10179 | kSelfAddress, kPeerAddress, |
| 10180 | QuicReceivedPacket(reinterpret_cast<char*>(retry_packet), |
| 10181 | retry_packet_length, clock_.Now())); |
| 10182 | |
| 10183 | if (invalid_retry_tag) { |
| 10184 | // Make sure we refuse to process a RETRY with invalid tag. |
| 10185 | EXPECT_FALSE(connection_.GetStats().retry_packet_processed); |
| 10186 | EXPECT_EQ(connection_.connection_id(), original_connection_id); |
| 10187 | EXPECT_TRUE(QuicPacketCreatorPeer::GetRetryToken( |
| 10188 | QuicConnectionPeer::GetPacketCreator(&connection_)) |
| 10189 | .empty()); |
| 10190 | return; |
| 10191 | } |
| 10192 | |
| 10193 | // Make sure we correctly parsed the RETRY. |
| 10194 | EXPECT_TRUE(connection_.GetStats().retry_packet_processed); |
| 10195 | EXPECT_EQ(connection_.connection_id(), new_connection_id); |
| 10196 | EXPECT_EQ(QuicPacketCreatorPeer::GetRetryToken( |
| 10197 | QuicConnectionPeer::GetPacketCreator(&connection_)), |
| 10198 | retry_token); |
| 10199 | |
| 10200 | // Test validating the original_connection_id from the config. |
| 10201 | QuicConfig received_config; |
| 10202 | QuicConfigPeer::SetNegotiated(&received_config, true); |
| 10203 | if (connection_.version().UsesTls()) { |
| 10204 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 10205 | &received_config, connection_.connection_id()); |
| 10206 | if (!missing_retry_id_in_config) { |
| 10207 | QuicConfigPeer::SetReceivedRetrySourceConnectionId( |
| 10208 | &received_config, config_retry_source_connection_id); |
| 10209 | } |
| 10210 | } |
| 10211 | if (!missing_original_id_in_config) { |
| 10212 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 10213 | &received_config, config_original_connection_id); |
| 10214 | } |
| 10215 | |
| 10216 | if (missing_original_id_in_config || wrong_original_id_in_config || |
| 10217 | missing_retry_id_in_config || wrong_retry_id_in_config) { |
| 10218 | EXPECT_CALL(visitor_, |
| 10219 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10220 | .Times(1); |
| 10221 | } else { |
| 10222 | EXPECT_CALL(visitor_, |
| 10223 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10224 | .Times(0); |
| 10225 | } |
| 10226 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10227 | connection_.SetFromConfig(received_config); |
| 10228 | if (missing_original_id_in_config || wrong_original_id_in_config || |
| 10229 | missing_retry_id_in_config || wrong_retry_id_in_config) { |
| 10230 | ASSERT_FALSE(connection_.connected()); |
| 10231 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 10232 | } else { |
| 10233 | EXPECT_TRUE(connection_.connected()); |
| 10234 | } |
| 10235 | } |
| 10236 | |
| 10237 | TEST_P(QuicConnectionTest, ClientParsesRetry) { |
| 10238 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10239 | /*missing_original_id_in_config=*/false, |
| 10240 | /*wrong_original_id_in_config=*/false, |
| 10241 | /*missing_retry_id_in_config=*/false, |
| 10242 | /*wrong_retry_id_in_config=*/false); |
| 10243 | } |
| 10244 | |
| 10245 | TEST_P(QuicConnectionTest, ClientParsesRetryInvalidTag) { |
| 10246 | TestClientRetryHandling(/*invalid_retry_tag=*/true, |
| 10247 | /*missing_original_id_in_config=*/false, |
| 10248 | /*wrong_original_id_in_config=*/false, |
| 10249 | /*missing_retry_id_in_config=*/false, |
| 10250 | /*wrong_retry_id_in_config=*/false); |
| 10251 | } |
| 10252 | |
| 10253 | TEST_P(QuicConnectionTest, ClientParsesRetryMissingOriginalId) { |
| 10254 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10255 | /*missing_original_id_in_config=*/true, |
| 10256 | /*wrong_original_id_in_config=*/false, |
| 10257 | /*missing_retry_id_in_config=*/false, |
| 10258 | /*wrong_retry_id_in_config=*/false); |
| 10259 | } |
| 10260 | |
| 10261 | TEST_P(QuicConnectionTest, ClientParsesRetryWrongOriginalId) { |
| 10262 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10263 | /*missing_original_id_in_config=*/false, |
| 10264 | /*wrong_original_id_in_config=*/true, |
| 10265 | /*missing_retry_id_in_config=*/false, |
| 10266 | /*wrong_retry_id_in_config=*/false); |
| 10267 | } |
| 10268 | |
| 10269 | TEST_P(QuicConnectionTest, ClientParsesRetryMissingRetryId) { |
| 10270 | if (!connection_.version().UsesTls()) { |
| 10271 | // Versions that do not authenticate connection IDs never send the |
| 10272 | // retry_source_connection_id transport parameter. |
| 10273 | return; |
| 10274 | } |
| 10275 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10276 | /*missing_original_id_in_config=*/false, |
| 10277 | /*wrong_original_id_in_config=*/false, |
| 10278 | /*missing_retry_id_in_config=*/true, |
| 10279 | /*wrong_retry_id_in_config=*/false); |
| 10280 | } |
| 10281 | |
| 10282 | TEST_P(QuicConnectionTest, ClientParsesRetryWrongRetryId) { |
| 10283 | if (!connection_.version().UsesTls()) { |
| 10284 | // Versions that do not authenticate connection IDs never send the |
| 10285 | // retry_source_connection_id transport parameter. |
| 10286 | return; |
| 10287 | } |
| 10288 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10289 | /*missing_original_id_in_config=*/false, |
| 10290 | /*wrong_original_id_in_config=*/false, |
| 10291 | /*missing_retry_id_in_config=*/false, |
| 10292 | /*wrong_retry_id_in_config=*/true); |
| 10293 | } |
| 10294 | |
| 10295 | TEST_P(QuicConnectionTest, ClientRetransmitsInitialPacketsOnRetry) { |
| 10296 | if (!connection_.version().HasIetfQuicFrames()) { |
| 10297 | // TestClientRetryHandling() currently only supports IETF draft versions. |
| 10298 | return; |
| 10299 | } |
| 10300 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10301 | |
| 10302 | connection_.SendCryptoStreamData(); |
| 10303 | |
| 10304 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10305 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10306 | /*missing_original_id_in_config=*/false, |
| 10307 | /*wrong_original_id_in_config=*/false, |
| 10308 | /*missing_retry_id_in_config=*/false, |
| 10309 | /*wrong_retry_id_in_config=*/false); |
| 10310 | |
| 10311 | // Verify that initial data is retransmitted immediately after receiving |
| 10312 | // RETRY. |
| 10313 | if (GetParam().ack_response == AckResponse::kImmediate) { |
| 10314 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 10315 | EXPECT_EQ(1u, writer_->framer()->crypto_frames().size()); |
| 10316 | } |
| 10317 | } |
| 10318 | |
| 10319 | TEST_P(QuicConnectionTest, NoInitialPacketsRetransmissionOnInvalidRetry) { |
| 10320 | if (!connection_.version().HasIetfQuicFrames()) { |
| 10321 | return; |
| 10322 | } |
| 10323 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10324 | |
| 10325 | connection_.SendCryptoStreamData(); |
| 10326 | |
| 10327 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10328 | TestClientRetryHandling(/*invalid_retry_tag=*/true, |
| 10329 | /*missing_original_id_in_config=*/false, |
| 10330 | /*wrong_original_id_in_config=*/false, |
| 10331 | /*missing_retry_id_in_config=*/false, |
| 10332 | /*wrong_retry_id_in_config=*/false); |
| 10333 | |
| 10334 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10335 | } |
| 10336 | |
| 10337 | TEST_P(QuicConnectionTest, ClientReceivesOriginalConnectionIdWithoutRetry) { |
| 10338 | if (!connection_.version().UsesTls()) { |
| 10339 | // QUIC+TLS is required to transmit connection ID transport parameters. |
| 10340 | return; |
| 10341 | } |
| 10342 | if (connection_.version().UsesTls()) { |
| 10343 | // Versions that authenticate connection IDs always send the |
| 10344 | // original_destination_connection_id transport parameter. |
| 10345 | return; |
| 10346 | } |
| 10347 | // Make sure that receiving the original_destination_connection_id transport |
| 10348 | // parameter fails the handshake when no RETRY packet was received before it. |
| 10349 | QuicConfig received_config; |
| 10350 | QuicConfigPeer::SetNegotiated(&received_config, true); |
| 10351 | QuicConfigPeer::SetReceivedOriginalConnectionId(&received_config, |
| 10352 | TestConnectionId(0x12345)); |
| 10353 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10354 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10355 | .Times(1); |
| 10356 | connection_.SetFromConfig(received_config); |
| 10357 | EXPECT_FALSE(connection_.connected()); |
| 10358 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 10359 | } |
| 10360 | |
| 10361 | TEST_P(QuicConnectionTest, ClientReceivesRetrySourceConnectionIdWithoutRetry) { |
| 10362 | if (!connection_.version().UsesTls()) { |
| 10363 | // Versions that do not authenticate connection IDs never send the |
| 10364 | // retry_source_connection_id transport parameter. |
| 10365 | return; |
| 10366 | } |
| 10367 | // Make sure that receiving the retry_source_connection_id transport parameter |
| 10368 | // fails the handshake when no RETRY packet was received before it. |
| 10369 | QuicConfig received_config; |
| 10370 | QuicConfigPeer::SetNegotiated(&received_config, true); |
| 10371 | QuicConfigPeer::SetReceivedRetrySourceConnectionId(&received_config, |
| 10372 | TestConnectionId(0x12345)); |
| 10373 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10374 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10375 | .Times(1); |
| 10376 | connection_.SetFromConfig(received_config); |
| 10377 | EXPECT_FALSE(connection_.connected()); |
| 10378 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 10379 | } |
| 10380 | |
| 10381 | // Regression test for http://crbug/1047977 |
| 10382 | TEST_P(QuicConnectionTest, MaxStreamsFrameCausesConnectionClose) { |
| 10383 | if (!VersionHasIetfQuicFrames(connection_.transport_version())) { |
| 10384 | return; |
| 10385 | } |
| 10386 | // Received frame causes connection close. |
| 10387 | EXPECT_CALL(visitor_, OnMaxStreamsFrame(_)) |
| 10388 | .WillOnce(InvokeWithoutArgs([this]() { |
| 10389 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10390 | connection_.CloseConnection( |
| 10391 | QUIC_TOO_MANY_BUFFERED_CONTROL_FRAMES, "error", |
| 10392 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10393 | return true; |
| 10394 | })); |
| 10395 | QuicFrames frames; |
| 10396 | frames.push_back(QuicFrame(QuicMaxStreamsFrame())); |
| 10397 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10398 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10399 | } |
| 10400 | |
| 10401 | TEST_P(QuicConnectionTest, StreamsBlockedFrameCausesConnectionClose) { |
| 10402 | if (!VersionHasIetfQuicFrames(connection_.transport_version())) { |
| 10403 | return; |
| 10404 | } |
| 10405 | // Received frame causes connection close. |
| 10406 | EXPECT_CALL(visitor_, OnStreamsBlockedFrame(_)) |
| 10407 | .WillOnce(InvokeWithoutArgs([this]() { |
| 10408 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10409 | connection_.CloseConnection( |
| 10410 | QUIC_TOO_MANY_BUFFERED_CONTROL_FRAMES, "error", |
| 10411 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10412 | return true; |
| 10413 | })); |
| 10414 | QuicFrames frames; |
| 10415 | frames.push_back( |
| 10416 | QuicFrame(QuicStreamsBlockedFrame(kInvalidControlFrameId, 10, false))); |
| 10417 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10418 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10419 | } |
| 10420 | |
| 10421 | TEST_P(QuicConnectionTest, |
| 10422 | BundleAckWithConnectionCloseMultiplePacketNumberSpace) { |
| 10423 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10424 | return; |
| 10425 | } |
| 10426 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10427 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10428 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 10429 | // Receives packet 1000 in initial data. |
| 10430 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10431 | // Receives packet 2000 in application data. |
| 10432 | ProcessDataPacketAtLevel(2000, false, ENCRYPTION_FORWARD_SECURE); |
| 10433 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10434 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 10435 | connection_.CloseConnection( |
| 10436 | kQuicErrorCode, "Some random error message", |
| 10437 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10438 | |
| 10439 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 10440 | |
| 10441 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 10442 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 10443 | // Verify ack is bundled. |
| 10444 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 10445 | |
| 10446 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 10447 | // Each connection close packet should be sent in distinct UDP packets. |
| 10448 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 10449 | writer_->connection_close_packets()); |
| 10450 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 10451 | writer_->packets_write_attempts()); |
| 10452 | return; |
| 10453 | } |
| 10454 | |
| 10455 | // A single UDP packet should be sent with multiple connection close packets |
| 10456 | // coalesced together. |
| 10457 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10458 | |
| 10459 | // Only the first packet has been processed yet. |
| 10460 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 10461 | |
| 10462 | // ProcessPacket resets the visitor and frees the coalesced packet. |
| 10463 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 10464 | auto packet = writer_->coalesced_packet()->Clone(); |
| 10465 | writer_->framer()->ProcessPacket(*packet); |
| 10466 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 10467 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 10468 | // Verify ack is bundled. |
| 10469 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 10470 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 10471 | } |
| 10472 | |
| 10473 | // Regression test for b/151220135. |
| 10474 | TEST_P(QuicConnectionTest, SendPingWhenSkipPacketNumberForPto) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10475 | QuicConfig config; |
| 10476 | QuicTagVector connection_options; |
| 10477 | connection_options.push_back(kPTOS); |
| 10478 | connection_options.push_back(k1PTO); |
| 10479 | config.SetConnectionOptionsToSend(connection_options); |
| 10480 | if (connection_.version().UsesTls()) { |
| 10481 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 10482 | &config, kMaxAcceptedDatagramFrameSize); |
| 10483 | } |
| 10484 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10485 | connection_.SetFromConfig(config); |
| 10486 | connection_.OnHandshakeComplete(); |
| 10487 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10488 | |
| 10489 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, SendMessage("message")); |
| 10490 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10491 | |
| 10492 | // PTO fires, verify a PING packet gets sent because there is no data to |
| 10493 | // send. |
| 10494 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(3), _, _)); |
| 10495 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10496 | EXPECT_EQ(1u, connection_.GetStats().pto_count); |
| 10497 | EXPECT_EQ(0u, connection_.GetStats().crypto_retransmit_count); |
| 10498 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 10499 | } |
| 10500 | |
| 10501 | // Regression test for b/155757133 |
| 10502 | TEST_P(QuicConnectionTest, DonotChangeQueuedAcks) { |
| 10503 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10504 | return; |
| 10505 | } |
| 10506 | const size_t kMinRttMs = 40; |
| 10507 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 10508 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 10509 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 10510 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 10511 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10512 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10513 | // Discard INITIAL key. |
| 10514 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 10515 | connection_.NeuterUnencryptedPackets(); |
| 10516 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10517 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10518 | |
| 10519 | ProcessPacket(2); |
| 10520 | ProcessPacket(3); |
| 10521 | ProcessPacket(4); |
| 10522 | // Process a packet containing stream frame followed by ACK of packets 1. |
| 10523 | QuicFrames frames; |
| 10524 | frames.push_back(QuicFrame(QuicStreamFrame( |
| 10525 | QuicUtils::GetFirstBidirectionalStreamId( |
| 10526 | connection_.version().transport_version, Perspective::IS_CLIENT), |
| 10527 | false, 0u, absl::string_view()))); |
| 10528 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 10529 | frames.push_back(QuicFrame(&ack_frame)); |
| 10530 | // Receiving stream frame causes something to send. |
| 10531 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([this]() { |
| 10532 | connection_.SendControlFrame(QuicFrame(QuicWindowUpdateFrame(1, 0, 0))); |
| 10533 | // Verify now the queued ACK contains packet number 2. |
| 10534 | EXPECT_TRUE(QuicPacketCreatorPeer::QueuedFrames( |
| 10535 | QuicConnectionPeer::GetPacketCreator(&connection_))[0] |
| 10536 | .ack_frame->packets.Contains(QuicPacketNumber(2))); |
| 10537 | })); |
| 10538 | ProcessFramesPacketAtLevel(9, frames, ENCRYPTION_FORWARD_SECURE); |
| 10539 | EXPECT_TRUE(writer_->ack_frames()[0].packets.Contains(QuicPacketNumber(2))); |
| 10540 | } |
| 10541 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10542 | TEST_P(QuicConnectionTest, DoNotExtendIdleTimeOnUndecryptablePackets) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10543 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10544 | QuicConfig config; |
| 10545 | connection_.SetFromConfig(config); |
| 10546 | // Subtract a second from the idle timeout on the client side. |
| 10547 | QuicTime initial_deadline = |
| 10548 | clock_.ApproximateNow() + |
| 10549 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 10550 | EXPECT_EQ(initial_deadline, connection_.GetTimeoutAlarm()->deadline()); |
| 10551 | |
| 10552 | // Received an undecryptable packet. |
| 10553 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10554 | peer_framer_.SetEncrypter( |
| 10555 | ENCRYPTION_FORWARD_SECURE, |
| 10556 | std::make_unique<quic::NullEncrypter>(Perspective::IS_CLIENT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10557 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 10558 | // Verify deadline does not get extended. |
| 10559 | EXPECT_EQ(initial_deadline, connection_.GetTimeoutAlarm()->deadline()); |
| 10560 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
| 10561 | QuicTime::Delta delay = initial_deadline - clock_.ApproximateNow(); |
| 10562 | clock_.AdvanceTime(delay); |
| 10563 | connection_.GetTimeoutAlarm()->Fire(); |
| 10564 | // Verify connection gets closed. |
| 10565 | EXPECT_FALSE(connection_.connected()); |
| 10566 | } |
| 10567 | |
| 10568 | TEST_P(QuicConnectionTest, BundleAckWithImmediateResponse) { |
| 10569 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10570 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10571 | |
| 10572 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([this]() { |
| 10573 | notifier_.WriteOrBufferWindowUpate(0, 0); |
| 10574 | })); |
| 10575 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10576 | ProcessDataPacket(1); |
| 10577 | // Verify ACK is bundled with WINDOW_UPDATE. |
| 10578 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 10579 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 10580 | } |
| 10581 | |
| 10582 | TEST_P(QuicConnectionTest, AckAlarmFiresEarly) { |
| 10583 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10584 | return; |
| 10585 | } |
| 10586 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10587 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10588 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10589 | } |
| 10590 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10591 | // Receives packet 1000 in initial data. |
| 10592 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10593 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10594 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10595 | peer_framer_.SetEncrypter( |
| 10596 | ENCRYPTION_ZERO_RTT, |
| 10597 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10598 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10599 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10600 | // Receives packet 1000 in application data. |
| 10601 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_ZERO_RTT); |
| 10602 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10603 | // Verify ACK deadline does not change. |
| 10604 | EXPECT_EQ(clock_.ApproximateNow() + kAlarmGranularity, |
| 10605 | connection_.GetAckAlarm()->deadline()); |
| 10606 | |
| 10607 | // Ack alarm fires early. |
| 10608 | // Verify the earliest ACK is flushed. |
| 10609 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10610 | connection_.GetAckAlarm()->Fire(); |
| 10611 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10612 | EXPECT_EQ(clock_.ApproximateNow() + DefaultDelayedAckTime(), |
| 10613 | connection_.GetAckAlarm()->deadline()); |
| 10614 | } |
| 10615 | |
| 10616 | TEST_P(QuicConnectionTest, ClientOnlyBlackholeDetectionClient) { |
| 10617 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 10618 | return; |
| 10619 | } |
| 10620 | QuicConfig config; |
| 10621 | QuicTagVector connection_options; |
| 10622 | connection_options.push_back(kCBHD); |
| 10623 | config.SetConnectionOptionsToSend(connection_options); |
| 10624 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10625 | connection_.SetFromConfig(config); |
| 10626 | EXPECT_CALL(visitor_, GetHandshakeState()) |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 10627 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 10628 | connection_.OnHandshakeComplete(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10629 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10630 | // Send stream data. |
| 10631 | SendStreamDataToPeer( |
| 10632 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10633 | 0, FIN, nullptr); |
| 10634 | // Verify blackhole detection is in progress. |
| 10635 | EXPECT_TRUE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10636 | } |
| 10637 | |
| 10638 | TEST_P(QuicConnectionTest, ClientOnlyBlackholeDetectionServer) { |
| 10639 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 10640 | return; |
| 10641 | } |
| 10642 | set_perspective(Perspective::IS_SERVER); |
| 10643 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 10644 | if (version().SupportsAntiAmplificationLimit()) { |
| 10645 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 10646 | } |
| 10647 | QuicConfig config; |
| 10648 | QuicTagVector connection_options; |
| 10649 | connection_options.push_back(kCBHD); |
| 10650 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 10651 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10652 | connection_.SetFromConfig(config); |
| 10653 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10654 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10655 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10656 | // Send stream data. |
| 10657 | SendStreamDataToPeer( |
| 10658 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10659 | 0, FIN, nullptr); |
| 10660 | // Verify blackhole detection is disabled. |
| 10661 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10662 | } |
| 10663 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10664 | // Regresstion test for b/158491591. |
| 10665 | TEST_P(QuicConnectionTest, MadeForwardProgressOnDiscardingKeys) { |
| 10666 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10667 | return; |
| 10668 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10669 | // Send handshake packet. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10670 | connection_.SetEncrypter( |
| 10671 | ENCRYPTION_HANDSHAKE, |
| 10672 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10673 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10674 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10675 | QuicConfig config; |
| 10676 | QuicTagVector connection_options; |
| 10677 | connection_options.push_back(k5RTO); |
| 10678 | config.SetConnectionOptionsToSend(connection_options); |
| 10679 | QuicConfigPeer::SetNegotiated(&config, true); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 10680 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2) || |
| 10681 | GetQuicReloadableFlag( |
| 10682 | quic_no_path_degrading_before_handshake_confirmed)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10683 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10684 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10685 | } |
| 10686 | if (connection_.version().UsesTls()) { |
| 10687 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 10688 | &config, connection_.connection_id()); |
| 10689 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 10690 | &config, connection_.connection_id()); |
| 10691 | } |
| 10692 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10693 | connection_.SetFromConfig(config); |
| 10694 | |
| 10695 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 10696 | if (GetQuicReloadableFlag( |
| 10697 | quic_no_path_degrading_before_handshake_confirmed)) { |
| 10698 | // No blackhole detection before handshake confirmed. |
| 10699 | EXPECT_FALSE(connection_.BlackholeDetectionInProgress()); |
| 10700 | } else { |
| 10701 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 10702 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10703 | // Discard handshake keys. |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 10704 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10705 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10706 | connection_.OnHandshakeComplete(); |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 10707 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2) || |
| 10708 | GetQuicReloadableFlag( |
| 10709 | quic_no_path_degrading_before_handshake_confirmed)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10710 | // Verify blackhole detection stops. |
| 10711 | EXPECT_FALSE(connection_.BlackholeDetectionInProgress()); |
| 10712 | } else { |
| 10713 | // Problematic: although there is nothing in flight, blackhole detection is |
| 10714 | // still in progress. |
| 10715 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 10716 | } |
| 10717 | } |
| 10718 | |
| 10719 | TEST_P(QuicConnectionTest, ProcessUndecryptablePacketsBasedOnEncryptionLevel) { |
| 10720 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10721 | return; |
| 10722 | } |
| 10723 | // SetFromConfig is always called after construction from InitializeSession. |
| 10724 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10725 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10726 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(AnyNumber()); |
| 10727 | QuicConfig config; |
| 10728 | connection_.SetFromConfig(config); |
| 10729 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10730 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10731 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10732 | peer_framer_.SetEncrypter( |
| 10733 | ENCRYPTION_HANDSHAKE, |
| 10734 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
| 10735 | peer_framer_.SetEncrypter( |
| 10736 | ENCRYPTION_FORWARD_SECURE, |
| 10737 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10738 | |
| 10739 | for (uint64_t i = 1; i <= 3; ++i) { |
| 10740 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 10741 | } |
| 10742 | ProcessDataPacketAtLevel(4, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 10743 | for (uint64_t j = 5; j <= 7; ++j) { |
| 10744 | ProcessDataPacketAtLevel(j, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 10745 | } |
| 10746 | EXPECT_EQ(7u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 10747 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 10748 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10749 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10750 | EXPECT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10751 | connection_.SetEncrypter( |
| 10752 | ENCRYPTION_HANDSHAKE, |
| 10753 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10754 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10755 | // Verify all ENCRYPTION_HANDSHAKE packets get processed. |
| 10756 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 10757 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(6); |
| 10758 | } |
| 10759 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 10760 | EXPECT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 10761 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10762 | SetDecrypter( |
| 10763 | ENCRYPTION_FORWARD_SECURE, |
| 10764 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10765 | EXPECT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 10766 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10767 | connection_.SetEncrypter( |
| 10768 | ENCRYPTION_FORWARD_SECURE, |
| 10769 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10770 | // Verify the 1-RTT packet gets processed. |
| 10771 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 10772 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 10773 | EXPECT_EQ(0u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 10774 | } |
| 10775 | |
| 10776 | TEST_P(QuicConnectionTest, ServerBundlesInitialDataWithInitialAck) { |
| 10777 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10778 | return; |
| 10779 | } |
| 10780 | set_perspective(Perspective::IS_SERVER); |
| 10781 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10782 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10783 | } |
| 10784 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10785 | // Receives packet 1000 in initial data. |
| 10786 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10787 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10788 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10789 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10790 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 10791 | QuicTime expected_pto_time = |
| 10792 | connection_.sent_packet_manager().GetRetransmissionTime(); |
| 10793 | |
| 10794 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 10795 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 10796 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10797 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10798 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10799 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 10800 | // Verify PTO time does not change. |
| 10801 | EXPECT_EQ(expected_pto_time, |
| 10802 | connection_.sent_packet_manager().GetRetransmissionTime()); |
| 10803 | |
| 10804 | // Receives packet 1001 in initial data. |
| 10805 | ProcessCryptoPacketAtLevel(1001, ENCRYPTION_INITIAL); |
| 10806 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10807 | // Receives packet 1002 in initial data. |
| 10808 | ProcessCryptoPacketAtLevel(1002, ENCRYPTION_INITIAL); |
| 10809 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 10810 | // Verify CRYPTO frame is bundled with INITIAL ACK. |
| 10811 | EXPECT_FALSE(writer_->crypto_frames().empty()); |
| 10812 | // Verify PTO time changes. |
| 10813 | EXPECT_NE(expected_pto_time, |
| 10814 | connection_.sent_packet_manager().GetRetransmissionTime()); |
| 10815 | } |
| 10816 | |
| 10817 | TEST_P(QuicConnectionTest, ClientBundlesHandshakeDataWithHandshakeAck) { |
| 10818 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10819 | return; |
| 10820 | } |
| 10821 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 10822 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10823 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10824 | } |
| 10825 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10826 | connection_.SetEncrypter( |
| 10827 | ENCRYPTION_HANDSHAKE, |
| 10828 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10829 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10830 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10831 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
| 10832 | peer_framer_.SetEncrypter( |
| 10833 | ENCRYPTION_HANDSHAKE, |
| 10834 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10835 | // Receives packet 1000 in handshake data. |
| 10836 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_HANDSHAKE); |
| 10837 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10838 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10839 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 10840 | |
| 10841 | // Receives packet 1001 in handshake data. |
| 10842 | ProcessCryptoPacketAtLevel(1001, ENCRYPTION_HANDSHAKE); |
| 10843 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10844 | // Receives packet 1002 in handshake data. |
| 10845 | ProcessCryptoPacketAtLevel(1002, ENCRYPTION_HANDSHAKE); |
| 10846 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 10847 | // Verify CRYPTO frame is bundled with HANDSHAKE ACK. |
| 10848 | EXPECT_FALSE(writer_->crypto_frames().empty()); |
| 10849 | } |
| 10850 | |
| 10851 | // Regresstion test for b/156232673. |
| 10852 | TEST_P(QuicConnectionTest, CoalescePacketOfLowerEncryptionLevel) { |
| 10853 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 10854 | return; |
| 10855 | } |
| 10856 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10857 | { |
| 10858 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10859 | connection_.SetEncrypter( |
| 10860 | ENCRYPTION_HANDSHAKE, |
| 10861 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
| 10862 | connection_.SetEncrypter( |
| 10863 | ENCRYPTION_FORWARD_SECURE, |
| 10864 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10865 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10866 | SendStreamDataToPeer(2, std::string(1286, 'a'), 0, NO_FIN, nullptr); |
| 10867 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10868 | // Try to coalesce a HANDSHAKE packet after 1-RTT packet. |
| 10869 | // Verify soft max packet length gets resumed and handshake packet gets |
| 10870 | // successfully sent. |
| 10871 | connection_.SendCryptoDataWithString("a", 0, ENCRYPTION_HANDSHAKE); |
| 10872 | } |
| 10873 | } |
| 10874 | |
| 10875 | // Regression test for b/160790422. |
| 10876 | TEST_P(QuicConnectionTest, ServerRetransmitsHandshakeDataEarly) { |
| 10877 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10878 | return; |
| 10879 | } |
| 10880 | set_perspective(Perspective::IS_SERVER); |
| 10881 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10882 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10883 | } |
| 10884 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10885 | // Receives packet 1000 in initial data. |
| 10886 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10887 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10888 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10889 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10890 | // Send INITIAL 1. |
| 10891 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 10892 | QuicTime expected_pto_time = |
| 10893 | connection_.sent_packet_manager().GetRetransmissionTime(); |
| 10894 | |
| 10895 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10896 | connection_.SetEncrypter( |
| 10897 | ENCRYPTION_HANDSHAKE, |
| 10898 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10899 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10900 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10901 | // Send HANDSHAKE 2 and 3. |
| 10902 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 10903 | connection_.SendCryptoDataWithString("bar", 3, ENCRYPTION_HANDSHAKE); |
| 10904 | // Verify PTO time does not change. |
| 10905 | EXPECT_EQ(expected_pto_time, |
| 10906 | connection_.sent_packet_manager().GetRetransmissionTime()); |
| 10907 | |
| 10908 | // Receives ACK for HANDSHAKE 2. |
| 10909 | QuicFrames frames; |
| 10910 | auto ack_frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 10911 | frames.push_back(QuicFrame(&ack_frame)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 10912 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10913 | ProcessFramesPacketAtLevel(30, frames, ENCRYPTION_HANDSHAKE); |
| 10914 | // Discard INITIAL key. |
| 10915 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 10916 | connection_.NeuterUnencryptedPackets(); |
| 10917 | // Receives PING from peer. |
| 10918 | frames.clear(); |
| 10919 | frames.push_back(QuicFrame(QuicPingFrame())); |
| 10920 | frames.push_back(QuicFrame(QuicPaddingFrame(3))); |
| 10921 | ProcessFramesPacketAtLevel(31, frames, ENCRYPTION_HANDSHAKE); |
| 10922 | EXPECT_EQ(clock_.Now() + kAlarmGranularity, |
| 10923 | connection_.GetAckAlarm()->deadline()); |
| 10924 | // Fire ACK alarm. |
| 10925 | clock_.AdvanceTime(kAlarmGranularity); |
| 10926 | connection_.GetAckAlarm()->Fire(); |
| 10927 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 10928 | // Verify handshake data gets retransmitted early. |
| 10929 | EXPECT_FALSE(writer_->crypto_frames().empty()); |
| 10930 | } |
| 10931 | |
| 10932 | // Regression test for b/161228202 |
| 10933 | TEST_P(QuicConnectionTest, InflatedRttSample) { |
| 10934 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10935 | return; |
| 10936 | } |
| 10937 | // 30ms RTT. |
| 10938 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 10939 | set_perspective(Perspective::IS_SERVER); |
| 10940 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10941 | // Receives packet 1000 in initial data. |
| 10942 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10943 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10944 | } |
| 10945 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 10946 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10947 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 10948 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10949 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10950 | // Send INITIAL 1. |
| 10951 | std::string initial_crypto_data(512, 'a'); |
| 10952 | connection_.SendCryptoDataWithString(initial_crypto_data, 0, |
| 10953 | ENCRYPTION_INITIAL); |
| 10954 | ASSERT_TRUE(connection_.sent_packet_manager() |
| 10955 | .GetRetransmissionTime() |
| 10956 | .IsInitialized()); |
| 10957 | QuicTime::Delta pto_timeout = |
| 10958 | connection_.sent_packet_manager().GetRetransmissionTime() - clock_.Now(); |
| 10959 | // Send Handshake 2. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10960 | connection_.SetEncrypter( |
| 10961 | ENCRYPTION_HANDSHAKE, |
| 10962 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10963 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10964 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 10965 | std::string handshake_crypto_data(1024, 'a'); |
| 10966 | connection_.SendCryptoDataWithString(handshake_crypto_data, 0, |
| 10967 | ENCRYPTION_HANDSHAKE); |
| 10968 | |
| 10969 | // INITIAL 1 gets lost and PTO fires. |
| 10970 | clock_.AdvanceTime(pto_timeout); |
| 10971 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10972 | |
| 10973 | clock_.AdvanceTime(kTestRTT); |
| 10974 | // Assume retransmitted INITIAL gets received. |
| 10975 | QuicFrames frames; |
| 10976 | auto ack_frame = InitAckFrame({{QuicPacketNumber(4), QuicPacketNumber(5)}}); |
| 10977 | frames.push_back(QuicFrame(&ack_frame)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 10978 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10979 | .Times(AnyNumber()); |
| 10980 | ProcessFramesPacketAtLevel(1001, frames, ENCRYPTION_INITIAL); |
| 10981 | EXPECT_EQ(kTestRTT, rtt_stats->latest_rtt()); |
| 10982 | // Because retransmitted INITIAL gets received so HANDSHAKE 2 gets processed. |
| 10983 | frames.clear(); |
| 10984 | // HANDSHAKE 5 is also processed. |
| 10985 | QuicAckFrame ack_frame2 = |
| 10986 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}, |
| 10987 | {QuicPacketNumber(5), QuicPacketNumber(6)}}); |
| 10988 | ack_frame2.ack_delay_time = QuicTime::Delta::Zero(); |
| 10989 | frames.push_back(QuicFrame(&ack_frame2)); |
| 10990 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_HANDSHAKE); |
| 10991 | // Verify RTT inflation gets mitigated. |
| 10992 | EXPECT_EQ(rtt_stats->latest_rtt(), kTestRTT); |
| 10993 | } |
| 10994 | |
| 10995 | // Regression test for b/161228202 |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 10996 | TEST_P(QuicConnectionTest, CoalescingPacketCausesInfiniteLoop) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10997 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10998 | return; |
| 10999 | } |
| 11000 | set_perspective(Perspective::IS_SERVER); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11001 | // Receives packet 1000 in initial data. |
| 11002 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 11003 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 11004 | } |
| 11005 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 11006 | |
| 11007 | // Set anti amplification factor to 2, such that RetransmitDataOfSpaceIfAny |
| 11008 | // makes no forward progress and causes infinite loop. |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 11009 | SetQuicFlag(quic_anti_amplification_factor, 2); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11010 | |
| 11011 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 11012 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 11013 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11014 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 11015 | // Send INITIAL 1. |
| 11016 | std::string initial_crypto_data(512, 'a'); |
| 11017 | connection_.SendCryptoDataWithString(initial_crypto_data, 0, |
| 11018 | ENCRYPTION_INITIAL); |
| 11019 | ASSERT_TRUE(connection_.sent_packet_manager() |
| 11020 | .GetRetransmissionTime() |
| 11021 | .IsInitialized()); |
| 11022 | QuicTime::Delta pto_timeout = |
| 11023 | connection_.sent_packet_manager().GetRetransmissionTime() - clock_.Now(); |
| 11024 | // Send Handshake 2. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 11025 | connection_.SetEncrypter( |
| 11026 | ENCRYPTION_HANDSHAKE, |
| 11027 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11028 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 11029 | // Verify HANDSHAKE packet is coalesced with INITIAL retransmission. |
| 11030 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 11031 | std::string handshake_crypto_data(1024, 'a'); |
| 11032 | connection_.SendCryptoDataWithString(handshake_crypto_data, 0, |
| 11033 | ENCRYPTION_HANDSHAKE); |
| 11034 | |
| 11035 | // INITIAL 1 gets lost and PTO fires. |
| 11036 | clock_.AdvanceTime(pto_timeout); |
| 11037 | connection_.GetRetransmissionAlarm()->Fire(); |
| 11038 | } |
| 11039 | |
| 11040 | TEST_P(QuicConnectionTest, ClientAckDelayForAsyncPacketProcessing) { |
| 11041 | if (!version().HasIetfQuicFrames()) { |
| 11042 | return; |
| 11043 | } |
| 11044 | // SetFromConfig is always called after construction from InitializeSession. |
| 11045 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 11046 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11047 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).WillOnce(Invoke([this]() { |
| 11048 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 11049 | connection_.NeuterUnencryptedPackets(); |
| 11050 | })); |
| 11051 | QuicConfig config; |
| 11052 | connection_.SetFromConfig(config); |
| 11053 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 11054 | peer_framer_.SetEncrypter( |
| 11055 | ENCRYPTION_HANDSHAKE, |
| 11056 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11057 | EXPECT_EQ(0u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 11058 | |
| 11059 | // Received undecryptable HANDSHAKE 2. |
| 11060 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 11061 | ASSERT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 11062 | // Received INITIAL 4 (which is retransmission of INITIAL 1) after 100ms. |
| 11063 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100)); |
| 11064 | ProcessDataPacketAtLevel(4, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 11065 | // Generate HANDSHAKE key. |
| 11066 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 11067 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11068 | EXPECT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 11069 | connection_.SetEncrypter( |
| 11070 | ENCRYPTION_HANDSHAKE, |
| 11071 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11072 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 11073 | // Verify HANDSHAKE packet gets processed. |
fayang | fea655c | 2022-05-17 08:19:12 -0700 | [diff] [blame] | 11074 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11075 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
fayang | fea655c | 2022-05-17 08:19:12 -0700 | [diff] [blame] | 11076 | // Verify immediate ACK has been sent out when flush went out of scope. |
| 11077 | ASSERT_FALSE(connection_.HasPendingAcks()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11078 | ASSERT_FALSE(writer_->ack_frames().empty()); |
fayang | fea655c | 2022-05-17 08:19:12 -0700 | [diff] [blame] | 11079 | // Verify the ack_delay_time in the sent HANDSHAKE ACK frame is 100ms. |
| 11080 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100), |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11081 | writer_->ack_frames()[0].ack_delay_time); |
| 11082 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 11083 | } |
| 11084 | |
| 11085 | TEST_P(QuicConnectionTest, TestingLiveness) { |
| 11086 | const size_t kMinRttMs = 40; |
| 11087 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11088 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 11089 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11090 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11091 | QuicConfig config; |
| 11092 | |
| 11093 | CryptoHandshakeMessage msg; |
| 11094 | std::string error_details; |
| 11095 | QuicConfig client_config; |
| 11096 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 11097 | kInitialStreamFlowControlWindowForTest); |
| 11098 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 11099 | kInitialSessionFlowControlWindowForTest); |
| 11100 | client_config.SetIdleNetworkTimeout(QuicTime::Delta::FromSeconds(30)); |
| 11101 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
| 11102 | const QuicErrorCode error = |
| 11103 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
| 11104 | EXPECT_THAT(error, IsQuicNoError()); |
| 11105 | |
| 11106 | if (connection_.version().UsesTls()) { |
| 11107 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 11108 | &config, connection_.connection_id()); |
| 11109 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 11110 | &config, connection_.connection_id()); |
| 11111 | } |
| 11112 | |
| 11113 | connection_.SetFromConfig(config); |
| 11114 | connection_.OnHandshakeComplete(); |
| 11115 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 11116 | ASSERT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 11117 | EXPECT_FALSE(connection_.MaybeTestLiveness()); |
| 11118 | |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 11119 | QuicTime deadline = QuicConnectionPeer::GetIdleNetworkDeadline(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11120 | QuicTime::Delta timeout = deadline - clock_.ApproximateNow(); |
| 11121 | // Advance time to near the idle timeout. |
| 11122 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromMilliseconds(1)); |
| 11123 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 11124 | EXPECT_TRUE(connection_.MaybeTestLiveness()); |
| 11125 | // Verify idle deadline does not change. |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 11126 | EXPECT_EQ(deadline, QuicConnectionPeer::GetIdleNetworkDeadline(&connection_)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11127 | } |
| 11128 | |
fayang | 5783c33 | 2022-12-14 09:30:25 -0800 | [diff] [blame] | 11129 | TEST_P(QuicConnectionTest, DisableLivenessTesting) { |
| 11130 | const size_t kMinRttMs = 40; |
| 11131 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11132 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 11133 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11134 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11135 | QuicConfig config; |
| 11136 | |
| 11137 | CryptoHandshakeMessage msg; |
| 11138 | std::string error_details; |
| 11139 | QuicConfig client_config; |
| 11140 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 11141 | kInitialStreamFlowControlWindowForTest); |
| 11142 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 11143 | kInitialSessionFlowControlWindowForTest); |
| 11144 | client_config.SetIdleNetworkTimeout(QuicTime::Delta::FromSeconds(30)); |
| 11145 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
| 11146 | const QuicErrorCode error = |
| 11147 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
| 11148 | EXPECT_THAT(error, IsQuicNoError()); |
| 11149 | |
| 11150 | if (connection_.version().UsesTls()) { |
| 11151 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 11152 | &config, connection_.connection_id()); |
| 11153 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 11154 | &config, connection_.connection_id()); |
| 11155 | } |
| 11156 | |
| 11157 | connection_.SetFromConfig(config); |
| 11158 | connection_.OnHandshakeComplete(); |
| 11159 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 11160 | connection_.DisableLivenessTesting(); |
| 11161 | ASSERT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 11162 | EXPECT_FALSE(connection_.MaybeTestLiveness()); |
| 11163 | |
| 11164 | QuicTime deadline = QuicConnectionPeer::GetIdleNetworkDeadline(&connection_); |
| 11165 | QuicTime::Delta timeout = deadline - clock_.ApproximateNow(); |
| 11166 | // Advance time to near the idle timeout. |
| 11167 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromMilliseconds(1)); |
| 11168 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 11169 | EXPECT_FALSE(connection_.MaybeTestLiveness()); |
| 11170 | } |
| 11171 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11172 | TEST_P(QuicConnectionTest, SilentIdleTimeout) { |
| 11173 | set_perspective(Perspective::IS_SERVER); |
| 11174 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 11175 | if (version().SupportsAntiAmplificationLimit()) { |
| 11176 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 11177 | } |
| 11178 | |
| 11179 | QuicConfig config; |
| 11180 | QuicConfigPeer::SetNegotiated(&config, true); |
| 11181 | if (connection_.version().UsesTls()) { |
| 11182 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 11183 | &config, connection_.connection_id()); |
| 11184 | QuicConfigPeer::SetReceivedInitialSourceConnectionId(&config, |
| 11185 | QuicConnectionId()); |
| 11186 | } |
| 11187 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11188 | connection_.SetFromConfig(config); |
| 11189 | |
| 11190 | EXPECT_TRUE(connection_.connected()); |
| 11191 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 11192 | |
| 11193 | if (version().handshake_protocol == PROTOCOL_TLS1_3) { |
| 11194 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 11195 | } |
| 11196 | EXPECT_CALL(visitor_, |
| 11197 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 11198 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
haoyuewang | 7b43efb | 2022-04-20 16:26:03 -0700 | [diff] [blame] | 11199 | if (!QuicConnectionPeer::GetBandwidthUpdateTimeout(&connection_) |
| 11200 | .IsInfinite()) { |
| 11201 | // Fires the bandwidth update. |
| 11202 | connection_.GetTimeoutAlarm()->Fire(); |
| 11203 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11204 | connection_.GetTimeoutAlarm()->Fire(); |
| 11205 | // Verify the connection close packets get serialized and added to |
| 11206 | // termination packets list. |
| 11207 | EXPECT_NE(nullptr, |
| 11208 | QuicConnectionPeer::GetConnectionClosePacket(&connection_)); |
| 11209 | } |
| 11210 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 11211 | TEST_P(QuicConnectionTest, DoNotSendPing) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11212 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 11213 | connection_.OnHandshakeComplete(); |
| 11214 | EXPECT_TRUE(connection_.connected()); |
| 11215 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 11216 | .WillRepeatedly(Return(true)); |
| 11217 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 11218 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 11219 | |
| 11220 | SendStreamDataToPeer( |
| 11221 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 11222 | "GET /", 0, FIN, nullptr); |
| 11223 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 11224 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 11225 | EXPECT_EQ(QuicTime::Delta::FromSeconds(15), |
| 11226 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 11227 | |
| 11228 | // Now recevie an ACK and response of the previous packet, which will move the |
| 11229 | // ping alarm forward. |
| 11230 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 11231 | QuicFrames frames; |
| 11232 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 11233 | frames.push_back(QuicFrame(&ack_frame)); |
| 11234 | frames.push_back(QuicFrame(QuicStreamFrame( |
| 11235 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), true, |
| 11236 | 0u, absl::string_view()))); |
| 11237 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 11238 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11239 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 11240 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 11241 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 11242 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 11243 | // The ping timer is set slightly less than 15 seconds in the future, because |
| 11244 | // of the 1s ping timer alarm granularity. |
| 11245 | EXPECT_EQ( |
| 11246 | QuicTime::Delta::FromSeconds(15) - QuicTime::Delta::FromMilliseconds(5), |
| 11247 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 11248 | |
| 11249 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(15)); |
| 11250 | // Suppose now ShouldKeepConnectionAlive returns false. |
| 11251 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 11252 | .WillRepeatedly(Return(false)); |
| 11253 | // Verify PING does not get sent. |
| 11254 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 11255 | connection_.GetPingAlarm()->Fire(); |
| 11256 | } |
| 11257 | |
| 11258 | // Regression test for b/159698337 |
| 11259 | TEST_P(QuicConnectionTest, DuplicateAckCausesLostPackets) { |
| 11260 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 11261 | return; |
| 11262 | } |
| 11263 | // Finish handshake. |
| 11264 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 11265 | notifier_.NeuterUnencryptedData(); |
| 11266 | connection_.NeuterUnencryptedPackets(); |
| 11267 | connection_.OnHandshakeComplete(); |
| 11268 | EXPECT_CALL(visitor_, GetHandshakeState()) |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 11269 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11270 | |
| 11271 | std::string data(1200, 'a'); |
| 11272 | // Send data packets 1 - 5. |
| 11273 | for (size_t i = 0; i < 5; ++i) { |
| 11274 | SendStreamDataToPeer( |
| 11275 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), data, |
| 11276 | i * 1200, i == 4 ? FIN : NO_FIN, nullptr); |
| 11277 | } |
| 11278 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 11279 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 11280 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
| 11281 | .Times(3); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11282 | |
| 11283 | // ACK packet 5 and 1 and 2 are detected lost. |
| 11284 | QuicAckFrame frame = |
| 11285 | InitAckFrame({{QuicPacketNumber(5), QuicPacketNumber(6)}}); |
| 11286 | LostPacketVector lost_packets; |
| 11287 | lost_packets.push_back( |
| 11288 | LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize)); |
| 11289 | lost_packets.push_back( |
| 11290 | LostPacket(QuicPacketNumber(2), kMaxOutgoingPacketSize)); |
| 11291 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 11292 | .Times(AnyNumber()) |
| 11293 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 11294 | Return(LossDetectionInterface::DetectionStats()))); |
| 11295 | ProcessAckPacket(1, &frame); |
| 11296 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 11297 | QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm(); |
| 11298 | EXPECT_TRUE(retransmission_alarm->IsSet()); |
| 11299 | |
| 11300 | // ACK packet 1 - 5 and 7. |
| 11301 | QuicAckFrame frame2 = |
| 11302 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(6)}, |
| 11303 | {QuicPacketNumber(7), QuicPacketNumber(8)}}); |
| 11304 | ProcessAckPacket(2, &frame2); |
| 11305 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 11306 | |
| 11307 | // ACK packet 7 again and assume packet 6 is detected lost. |
| 11308 | QuicAckFrame frame3 = |
| 11309 | InitAckFrame({{QuicPacketNumber(7), QuicPacketNumber(8)}}); |
| 11310 | lost_packets.clear(); |
| 11311 | lost_packets.push_back( |
| 11312 | LostPacket(QuicPacketNumber(6), kMaxOutgoingPacketSize)); |
| 11313 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 11314 | .Times(AnyNumber()) |
| 11315 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 11316 | Return(LossDetectionInterface::DetectionStats()))); |
| 11317 | ProcessAckPacket(3, &frame3); |
| 11318 | // Make sure loss detection is cancelled even there is no new acked packets. |
| 11319 | EXPECT_FALSE(connection_.BlackholeDetectionInProgress()); |
| 11320 | } |
| 11321 | |
| 11322 | TEST_P(QuicConnectionTest, ShorterIdleTimeoutOnSentPackets) { |
| 11323 | EXPECT_TRUE(connection_.connected()); |
| 11324 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11325 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100), |
| 11326 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11327 | |
| 11328 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11329 | QuicConfig config; |
| 11330 | config.SetClientConnectionOptions(QuicTagVector{kFIDT}); |
| 11331 | QuicConfigPeer::SetNegotiated(&config, true); |
| 11332 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 11333 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 11334 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 11335 | } |
| 11336 | if (connection_.version().UsesTls()) { |
| 11337 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 11338 | &config, connection_.connection_id()); |
| 11339 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 11340 | &config, connection_.connection_id()); |
| 11341 | } |
| 11342 | connection_.SetFromConfig(config); |
| 11343 | |
| 11344 | ASSERT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 11345 | // Send a packet close to timeout. |
| 11346 | QuicTime::Delta timeout = |
| 11347 | connection_.GetTimeoutAlarm()->deadline() - clock_.Now(); |
| 11348 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromSeconds(1)); |
| 11349 | // Send stream data. |
| 11350 | SendStreamDataToPeer( |
| 11351 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 11352 | 0, FIN, nullptr); |
| 11353 | // Verify this sent packet does not extend idle timeout since 1s is > PTO |
| 11354 | // delay. |
| 11355 | ASSERT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 11356 | EXPECT_EQ(QuicTime::Delta::FromSeconds(1), |
| 11357 | connection_.GetTimeoutAlarm()->deadline() - clock_.Now()); |
| 11358 | |
| 11359 | // Received an ACK 100ms later. |
| 11360 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromMilliseconds(100)); |
| 11361 | QuicAckFrame ack = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 11362 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11363 | ProcessAckPacket(1, &ack); |
| 11364 | // Verify idle timeout gets extended. |
| 11365 | EXPECT_EQ(clock_.Now() + timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 11366 | } |
| 11367 | |
| 11368 | // Regression test for b/166255274 |
| 11369 | TEST_P(QuicConnectionTest, |
| 11370 | ReserializeInitialPacketInCoalescerAfterDiscardingInitialKey) { |
| 11371 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 11372 | return; |
| 11373 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11374 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 11375 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 11376 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 11377 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 11378 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 11379 | std::make_unique<TaggingEncrypter>(0x02)); |
| 11380 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 11381 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).WillOnce(Invoke([this]() { |
| 11382 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 11383 | connection_.NeuterUnencryptedPackets(); |
| 11384 | })); |
| 11385 | { |
| 11386 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 11387 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 11388 | // Verify the packet is on hold. |
| 11389 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11390 | // Flush pending ACKs. |
| 11391 | connection_.GetAckAlarm()->Fire(); |
| 11392 | } |
| 11393 | EXPECT_FALSE(connection_.packet_creator().HasPendingFrames()); |
| 11394 | // The ACK frame is deleted along with initial_packet_ in coalescer. Sending |
| 11395 | // connection close would cause this (released) ACK frame be serialized (and |
| 11396 | // crashes). |
| 11397 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 11398 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_FORWARD_SECURE); |
| 11399 | EXPECT_TRUE(connection_.connected()); |
| 11400 | } |
| 11401 | |
| 11402 | TEST_P(QuicConnectionTest, PathValidationOnNewSocketSuccess) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11403 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11404 | return; |
| 11405 | } |
| 11406 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11407 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 11408 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 11409 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 11410 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11411 | .Times(AtLeast(1u)) |
| 11412 | .WillOnce(Invoke([&]() { |
| 11413 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11414 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 11415 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 11416 | EXPECT_EQ(kNewSelfAddress.host(), |
| 11417 | new_writer.last_write_source_address()); |
| 11418 | })); |
| 11419 | bool success = false; |
| 11420 | connection_.ValidatePath( |
| 11421 | std::make_unique<TestQuicPathValidationContext>( |
| 11422 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 11423 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11424 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 11425 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11426 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11427 | |
| 11428 | QuicFrames frames; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11429 | frames.push_back(QuicFrame(QuicPathResponseFrame( |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11430 | 99, new_writer.path_challenge_frames().front().data_buffer))); |
| 11431 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 11432 | ENCRYPTION_FORWARD_SECURE); |
| 11433 | EXPECT_TRUE(success); |
| 11434 | } |
| 11435 | |
| 11436 | TEST_P(QuicConnectionTest, NewPathValidationCancelsPreviousOne) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11437 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11438 | return; |
| 11439 | } |
| 11440 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11441 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 11442 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 11443 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 11444 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11445 | .Times(AtLeast(1u)) |
| 11446 | .WillOnce(Invoke([&]() { |
| 11447 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11448 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 11449 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 11450 | EXPECT_EQ(kNewSelfAddress.host(), |
| 11451 | new_writer.last_write_source_address()); |
| 11452 | })); |
| 11453 | bool success = true; |
| 11454 | connection_.ValidatePath( |
| 11455 | std::make_unique<TestQuicPathValidationContext>( |
| 11456 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 11457 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11458 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 11459 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11460 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11461 | |
| 11462 | // Start another path validation request. |
| 11463 | const QuicSocketAddress kNewSelfAddress2(QuicIpAddress::Any4(), 12346); |
| 11464 | EXPECT_NE(kNewSelfAddress2, connection_.self_address()); |
| 11465 | TestPacketWriter new_writer2(version(), &clock_, Perspective::IS_CLIENT); |
| 11466 | if (!connection_.connection_migration_use_new_cid()) { |
| 11467 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11468 | .Times(AtLeast(1u)) |
| 11469 | .WillOnce(Invoke([&]() { |
| 11470 | EXPECT_EQ(1u, new_writer2.packets_write_attempts()); |
| 11471 | EXPECT_EQ(1u, new_writer2.path_challenge_frames().size()); |
| 11472 | EXPECT_EQ(1u, new_writer2.padding_frames().size()); |
| 11473 | EXPECT_EQ(kNewSelfAddress2.host(), |
| 11474 | new_writer2.last_write_source_address()); |
| 11475 | })); |
| 11476 | } |
| 11477 | bool success2 = false; |
| 11478 | connection_.ValidatePath( |
| 11479 | std::make_unique<TestQuicPathValidationContext>( |
| 11480 | kNewSelfAddress2, connection_.peer_address(), &new_writer2), |
| 11481 | std::make_unique<TestValidationResultDelegate>( |
| 11482 | &connection_, kNewSelfAddress2, connection_.peer_address(), |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11483 | &success2), |
| 11484 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11485 | EXPECT_FALSE(success); |
| 11486 | if (connection_.connection_migration_use_new_cid()) { |
| 11487 | // There is no pening path validation as there is no available connection |
| 11488 | // ID. |
| 11489 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 11490 | } else { |
| 11491 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11492 | } |
| 11493 | } |
| 11494 | |
| 11495 | // Regression test for b/182571515. |
| 11496 | TEST_P(QuicConnectionTest, PathValidationRetry) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11497 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11498 | return; |
| 11499 | } |
| 11500 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11501 | |
| 11502 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11503 | .Times(2u) |
| 11504 | .WillRepeatedly(Invoke([&]() { |
| 11505 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 11506 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11507 | })); |
| 11508 | bool success = true; |
| 11509 | connection_.ValidatePath(std::make_unique<TestQuicPathValidationContext>( |
| 11510 | connection_.self_address(), |
| 11511 | connection_.peer_address(), writer_.get()), |
| 11512 | std::make_unique<TestValidationResultDelegate>( |
| 11513 | &connection_, connection_.self_address(), |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11514 | connection_.peer_address(), &success), |
| 11515 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11516 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11517 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11518 | |
| 11519 | // Retry after time out. |
| 11520 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 11521 | static_cast<test::MockRandom*>(helper_->GetRandomGenerator())->ChangeValue(); |
| 11522 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 11523 | QuicPathValidatorPeer::retry_timer( |
| 11524 | QuicConnectionPeer::path_validator(&connection_))) |
| 11525 | ->Fire(); |
| 11526 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 11527 | } |
| 11528 | |
| 11529 | TEST_P(QuicConnectionTest, PathValidationReceivesStatelessReset) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11530 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11531 | return; |
| 11532 | } |
| 11533 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11534 | QuicConfig config; |
| 11535 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, |
| 11536 | kTestStatelessResetToken); |
| 11537 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11538 | connection_.SetFromConfig(config); |
| 11539 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 11540 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 11541 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 11542 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11543 | .Times(AtLeast(1u)) |
| 11544 | .WillOnce(Invoke([&]() { |
| 11545 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11546 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 11547 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 11548 | EXPECT_EQ(kNewSelfAddress.host(), |
| 11549 | new_writer.last_write_source_address()); |
| 11550 | })); |
| 11551 | bool success = true; |
| 11552 | connection_.ValidatePath( |
| 11553 | std::make_unique<TestQuicPathValidationContext>( |
| 11554 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 11555 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11556 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 11557 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11558 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11559 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11560 | |
| 11561 | std::unique_ptr<QuicEncryptedPacket> packet( |
| 11562 | QuicFramer::BuildIetfStatelessResetPacket(connection_id_, |
| 11563 | /*received_packet_length=*/100, |
| 11564 | kTestStatelessResetToken)); |
| 11565 | std::unique_ptr<QuicReceivedPacket> received( |
| 11566 | ConstructReceivedPacket(*packet, QuicTime::Zero())); |
| 11567 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 11568 | connection_.ProcessUdpPacket(kNewSelfAddress, kPeerAddress, *received); |
| 11569 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 11570 | EXPECT_FALSE(success); |
| 11571 | } |
| 11572 | |
| 11573 | // Tests that PATH_CHALLENGE is dropped if it is sent via a blocked alternative |
| 11574 | // writer. |
| 11575 | TEST_P(QuicConnectionTest, SendPathChallengeUsingBlockedNewSocket) { |
| 11576 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version) || |
| 11577 | !connection_.connection_migration_use_new_cid()) { |
| 11578 | return; |
| 11579 | } |
| 11580 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11581 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 11582 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 11583 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 11584 | new_writer.BlockOnNextWrite(); |
| 11585 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(0); |
| 11586 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11587 | .Times(AtLeast(1)) |
| 11588 | .WillOnce(Invoke([&]() { |
| 11589 | // Even though the socket is blocked, the PATH_CHALLENGE should still be |
| 11590 | // treated as sent. |
| 11591 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11592 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 11593 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 11594 | EXPECT_EQ(kNewSelfAddress.host(), |
| 11595 | new_writer.last_write_source_address()); |
| 11596 | })); |
| 11597 | bool success = false; |
| 11598 | connection_.ValidatePath( |
| 11599 | std::make_unique<TestQuicPathValidationContext>( |
| 11600 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 11601 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11602 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 11603 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11604 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11605 | |
| 11606 | new_writer.SetWritable(); |
| 11607 | // Write event on the default socket shouldn't make any difference. |
| 11608 | connection_.OnCanWrite(); |
| 11609 | // A NEW_CONNECTION_ID frame is received in PathProbeTestInit and OnCanWrite |
| 11610 | // will write a acking packet. |
| 11611 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11612 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11613 | } |
| 11614 | |
| 11615 | // Tests that PATH_CHALLENGE is dropped if it is sent via the default writer |
| 11616 | // and the writer is blocked. |
| 11617 | TEST_P(QuicConnectionTest, SendPathChallengeUsingBlockedDefaultSocket) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11618 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11619 | return; |
| 11620 | } |
| 11621 | PathProbeTestInit(Perspective::IS_SERVER); |
| 11622 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Any4(), 12345); |
| 11623 | writer_->BlockOnNextWrite(); |
| 11624 | // 1st time is after writer returns WRITE_STATUS_BLOCKED. 2nd time is in |
| 11625 | // ShouldGeneratePacket(). |
| 11626 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(2)); |
| 11627 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 11628 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11629 | .Times(AtLeast(1u)) |
| 11630 | .WillOnce(Invoke([&]() { |
| 11631 | // This packet isn't sent actually, instead it is buffered in the |
| 11632 | // connection. |
| 11633 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11634 | if (connection_.validate_client_address()) { |
| 11635 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 11636 | EXPECT_EQ(0, |
| 11637 | memcmp(&path_challenge_payload, |
| 11638 | &writer_->path_response_frames().front().data_buffer, |
| 11639 | sizeof(path_challenge_payload))); |
| 11640 | } |
| 11641 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 11642 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11643 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11644 | })) |
| 11645 | .WillRepeatedly(Invoke([&]() { |
| 11646 | // Only one PATH_CHALLENGE should be sent out. |
| 11647 | EXPECT_EQ(0u, writer_->path_challenge_frames().size()); |
| 11648 | })); |
| 11649 | bool success = false; |
| 11650 | if (connection_.validate_client_address()) { |
| 11651 | // Receiving a PATH_CHALLENGE from the new peer address should trigger |
| 11652 | // address validation. |
| 11653 | QuicFrames frames; |
| 11654 | frames.push_back( |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11655 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11656 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 11657 | ENCRYPTION_FORWARD_SECURE); |
| 11658 | } else { |
| 11659 | // Manually start to validate the new peer address. |
| 11660 | connection_.ValidatePath( |
| 11661 | std::make_unique<TestQuicPathValidationContext>( |
| 11662 | connection_.self_address(), kNewPeerAddress, writer_.get()), |
| 11663 | std::make_unique<TestValidationResultDelegate>( |
| 11664 | &connection_, connection_.self_address(), kNewPeerAddress, |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11665 | &success), |
| 11666 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11667 | } |
| 11668 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11669 | |
| 11670 | // Try again with the new socket blocked from the beginning. The 2nd |
| 11671 | // PATH_CHALLENGE shouldn't be serialized, but be dropped. |
| 11672 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 11673 | static_cast<test::MockRandom*>(helper_->GetRandomGenerator())->ChangeValue(); |
| 11674 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 11675 | QuicPathValidatorPeer::retry_timer( |
| 11676 | QuicConnectionPeer::path_validator(&connection_))) |
| 11677 | ->Fire(); |
| 11678 | |
| 11679 | // No more write attempt should be made. |
| 11680 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11681 | |
| 11682 | writer_->SetWritable(); |
| 11683 | // OnCanWrite() should actually write out the 1st PATH_CHALLENGE packet |
| 11684 | // buffered earlier, thus incrementing the write counter. It may also send |
| 11685 | // ACKs to previously received packets. |
| 11686 | connection_.OnCanWrite(); |
| 11687 | EXPECT_LE(2u, writer_->packets_write_attempts()); |
| 11688 | } |
| 11689 | |
| 11690 | // Tests that write error on the alternate socket should be ignored. |
| 11691 | TEST_P(QuicConnectionTest, SendPathChallengeFailOnNewSocket) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11692 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11693 | return; |
| 11694 | } |
| 11695 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11696 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 11697 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 11698 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 11699 | new_writer.SetShouldWriteFail(); |
| 11700 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 11701 | .Times(0); |
| 11702 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0u); |
| 11703 | |
| 11704 | bool success = false; |
| 11705 | connection_.ValidatePath( |
| 11706 | std::make_unique<TestQuicPathValidationContext>( |
| 11707 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 11708 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11709 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 11710 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11711 | EXPECT_EQ(1u, new_writer.packets_write_attempts()); |
| 11712 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 11713 | EXPECT_EQ(1u, new_writer.padding_frames().size()); |
| 11714 | EXPECT_EQ(kNewSelfAddress.host(), new_writer.last_write_source_address()); |
| 11715 | |
| 11716 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 11717 | // Regardless of the write error, the connection should still be connected. |
| 11718 | EXPECT_TRUE(connection_.connected()); |
| 11719 | } |
| 11720 | |
| 11721 | // Tests that write error while sending PATH_CHALLANGE from the default socket |
| 11722 | // should close the connection. |
| 11723 | TEST_P(QuicConnectionTest, SendPathChallengeFailOnDefaultPath) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11724 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11725 | return; |
| 11726 | } |
| 11727 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11728 | |
| 11729 | writer_->SetShouldWriteFail(); |
| 11730 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 11731 | .WillOnce( |
| 11732 | Invoke([](QuicConnectionCloseFrame frame, ConnectionCloseSource) { |
| 11733 | EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, frame.quic_error_code); |
| 11734 | })); |
| 11735 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0u); |
| 11736 | { |
| 11737 | // Add a flusher to force flush, otherwise the frames will remain in the |
| 11738 | // packet creator. |
| 11739 | bool success = false; |
| 11740 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 11741 | connection_.ValidatePath(std::make_unique<TestQuicPathValidationContext>( |
| 11742 | connection_.self_address(), |
| 11743 | connection_.peer_address(), writer_.get()), |
| 11744 | std::make_unique<TestValidationResultDelegate>( |
| 11745 | &connection_, connection_.self_address(), |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11746 | connection_.peer_address(), &success), |
| 11747 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11748 | } |
| 11749 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11750 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 11751 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11752 | EXPECT_EQ(connection_.peer_address(), writer_->last_write_peer_address()); |
| 11753 | EXPECT_FALSE(connection_.connected()); |
| 11754 | // Closing connection should abandon ongoing path validation. |
| 11755 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 11756 | } |
| 11757 | |
| 11758 | TEST_P(QuicConnectionTest, SendPathChallengeFailOnAlternativePeerAddress) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11759 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11760 | return; |
| 11761 | } |
| 11762 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11763 | |
| 11764 | writer_->SetShouldWriteFail(); |
| 11765 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Any4(), 12345); |
| 11766 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 11767 | .WillOnce( |
| 11768 | Invoke([](QuicConnectionCloseFrame frame, ConnectionCloseSource) { |
| 11769 | EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, frame.quic_error_code); |
| 11770 | })); |
| 11771 | // Sending PATH_CHALLENGE to trigger a flush write which will fail and close |
| 11772 | // the connection. |
| 11773 | bool success = false; |
| 11774 | connection_.ValidatePath( |
| 11775 | std::make_unique<TestQuicPathValidationContext>( |
| 11776 | connection_.self_address(), kNewPeerAddress, writer_.get()), |
| 11777 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11778 | &connection_, connection_.self_address(), kNewPeerAddress, &success), |
| 11779 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11780 | |
| 11781 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 11782 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 11783 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 11784 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11785 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11786 | EXPECT_FALSE(connection_.connected()); |
| 11787 | } |
| 11788 | |
| 11789 | TEST_P(QuicConnectionTest, |
| 11790 | SendPathChallengeFailPacketTooBigOnAlternativePeerAddress) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 11791 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11792 | return; |
| 11793 | } |
| 11794 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 11795 | // Make sure there is no outstanding ACK_FRAME to write. |
| 11796 | connection_.OnCanWrite(); |
| 11797 | uint32_t num_packets_write_attempts = writer_->packets_write_attempts(); |
| 11798 | |
| 11799 | writer_->SetShouldWriteFail(); |
| 11800 | writer_->SetWriteError(*writer_->MessageTooBigErrorCode()); |
| 11801 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Any4(), 12345); |
| 11802 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 11803 | .Times(0u); |
| 11804 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0u); |
| 11805 | // Sending PATH_CHALLENGE to trigger a flush write which will fail with |
| 11806 | // MSG_TOO_BIG. |
| 11807 | bool success = false; |
| 11808 | connection_.ValidatePath( |
| 11809 | std::make_unique<TestQuicPathValidationContext>( |
| 11810 | connection_.self_address(), kNewPeerAddress, writer_.get()), |
| 11811 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 11812 | &connection_, connection_.self_address(), kNewPeerAddress, &success), |
| 11813 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11814 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11815 | // Connection shouldn't be closed. |
| 11816 | EXPECT_TRUE(connection_.connected()); |
| 11817 | EXPECT_EQ(++num_packets_write_attempts, writer_->packets_write_attempts()); |
| 11818 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 11819 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11820 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11821 | } |
| 11822 | |
| 11823 | // Check that if there are two PATH_CHALLENGE frames in the packet, the latter |
| 11824 | // one is ignored. |
| 11825 | TEST_P(QuicConnectionTest, ReceiveMultiplePathChallenge) { |
| 11826 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 11827 | return; |
| 11828 | } |
| 11829 | PathProbeTestInit(Perspective::IS_SERVER); |
| 11830 | |
| 11831 | QuicPathFrameBuffer path_frame_buffer1{0, 1, 2, 3, 4, 5, 6, 7}; |
| 11832 | QuicPathFrameBuffer path_frame_buffer2{8, 9, 10, 11, 12, 13, 14, 15}; |
| 11833 | QuicFrames frames; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11834 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer1))); |
| 11835 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer2))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11836 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback6(), |
| 11837 | /*port=*/23456); |
| 11838 | |
| 11839 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 11840 | |
| 11841 | // Expect 2 packets to be sent: the first are padded PATH_RESPONSE(s) to the |
| 11842 | // alternative peer address. The 2nd is a ACK-only packet to the original |
| 11843 | // peer address. |
| 11844 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11845 | .Times(2) |
| 11846 | .WillOnce(Invoke([=]() { |
| 11847 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 11848 | // The final check is to ensure that the random data in the response |
| 11849 | // matches the random data from the challenge. |
| 11850 | EXPECT_EQ(0, |
| 11851 | memcmp(path_frame_buffer1.data(), |
| 11852 | &(writer_->path_response_frames().front().data_buffer), |
| 11853 | sizeof(path_frame_buffer1))); |
| 11854 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11855 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11856 | })) |
| 11857 | .WillOnce(Invoke([=]() { |
| 11858 | // The last write of ACK-only packet should still use the old peer |
| 11859 | // address. |
| 11860 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 11861 | })); |
| 11862 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 11863 | ENCRYPTION_FORWARD_SECURE); |
| 11864 | } |
| 11865 | |
| 11866 | TEST_P(QuicConnectionTest, ReceiveStreamFrameBeforePathChallenge) { |
| 11867 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 11868 | return; |
| 11869 | } |
| 11870 | PathProbeTestInit(Perspective::IS_SERVER); |
| 11871 | |
| 11872 | QuicFrames frames; |
| 11873 | frames.push_back(QuicFrame(frame1_)); |
| 11874 | QuicPathFrameBuffer path_frame_buffer{0, 1, 2, 3, 4, 5, 6, 7}; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11875 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer))); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 11876 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11877 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 11878 | /*port=*/23456); |
| 11879 | |
| 11880 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)); |
| 11881 | EXPECT_CALL(*send_algorithm_, OnConnectionMigration()) |
| 11882 | .Times(connection_.validate_client_address() ? 0u : 1u); |
| 11883 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 11884 | .WillOnce(Invoke([=](const QuicStreamFrame& frame) { |
| 11885 | // Send some data on the stream. The STREAM_FRAME should be built into |
| 11886 | // one packet together with the latter PATH_RESPONSE and PATH_CHALLENGE. |
| 11887 | const std::string data{"response body"}; |
| 11888 | connection_.producer()->SaveStreamData(frame.stream_id, data); |
| 11889 | return notifier_.WriteOrBufferData(frame.stream_id, data.length(), |
| 11890 | NO_FIN); |
| 11891 | })); |
| 11892 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11893 | .Times(connection_.validate_client_address() ? 0u : 1u); |
| 11894 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 11895 | ENCRYPTION_FORWARD_SECURE); |
| 11896 | |
| 11897 | // Verify that this packet contains a STREAM_FRAME and a |
| 11898 | // PATH_RESPONSE_FRAME. |
| 11899 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 11900 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 11901 | EXPECT_EQ(connection_.validate_client_address() ? 1u : 0u, |
| 11902 | writer_->path_challenge_frames().size()); |
| 11903 | // The final check is to ensure that the random data in the response |
| 11904 | // matches the random data from the challenge. |
| 11905 | EXPECT_EQ(0, memcmp(path_frame_buffer.data(), |
| 11906 | &(writer_->path_response_frames().front().data_buffer), |
| 11907 | sizeof(path_frame_buffer))); |
| 11908 | EXPECT_EQ(connection_.validate_client_address() ? 1u : 0u, |
| 11909 | writer_->path_challenge_frames().size()); |
| 11910 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11911 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11912 | if (connection_.validate_client_address()) { |
| 11913 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11914 | } |
| 11915 | } |
| 11916 | |
| 11917 | TEST_P(QuicConnectionTest, ReceiveStreamFrameFollowingPathChallenge) { |
| 11918 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 11919 | return; |
| 11920 | } |
| 11921 | PathProbeTestInit(Perspective::IS_SERVER); |
| 11922 | |
| 11923 | QuicFrames frames; |
| 11924 | QuicPathFrameBuffer path_frame_buffer{0, 1, 2, 3, 4, 5, 6, 7}; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11925 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11926 | // PATH_RESPONSE should be flushed out before the rest packet is parsed. |
| 11927 | frames.push_back(QuicFrame(frame1_)); |
| 11928 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 11929 | /*port=*/23456); |
| 11930 | QuicByteCount received_packet_size; |
| 11931 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 11932 | .Times(AtLeast(1u)) |
| 11933 | .WillOnce(Invoke([=, &received_packet_size]() { |
| 11934 | // Verify that this packet contains a PATH_RESPONSE_FRAME. |
| 11935 | EXPECT_EQ(0u, writer_->stream_frames().size()); |
| 11936 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 11937 | // The final check is to ensure that the random data in the response |
| 11938 | // matches the random data from the challenge. |
| 11939 | EXPECT_EQ(0, |
| 11940 | memcmp(path_frame_buffer.data(), |
| 11941 | &(writer_->path_response_frames().front().data_buffer), |
| 11942 | sizeof(path_frame_buffer))); |
| 11943 | EXPECT_EQ(connection_.validate_client_address() ? 1u : 0u, |
| 11944 | writer_->path_challenge_frames().size()); |
| 11945 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 11946 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 11947 | received_packet_size = |
| 11948 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_); |
| 11949 | })); |
| 11950 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)); |
| 11951 | EXPECT_CALL(*send_algorithm_, OnConnectionMigration()) |
| 11952 | .Times(connection_.validate_client_address() ? 0u : 1u); |
| 11953 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 11954 | .WillOnce(Invoke([=](const QuicStreamFrame& frame) { |
| 11955 | // Send some data on the stream. The STREAM_FRAME should be built into a |
| 11956 | // new packet but throttled by anti-amplifciation limit. |
| 11957 | const std::string data{"response body"}; |
| 11958 | connection_.producer()->SaveStreamData(frame.stream_id, data); |
| 11959 | return notifier_.WriteOrBufferData(frame.stream_id, data.length(), |
| 11960 | NO_FIN); |
| 11961 | })); |
| 11962 | |
| 11963 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 11964 | ENCRYPTION_FORWARD_SECURE); |
| 11965 | if (!connection_.validate_client_address()) { |
| 11966 | return; |
| 11967 | } |
| 11968 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 11969 | EXPECT_EQ(0u, |
| 11970 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 11971 | EXPECT_EQ( |
| 11972 | received_packet_size, |
| 11973 | QuicConnectionPeer::BytesReceivedBeforeAddressValidation(&connection_)); |
| 11974 | } |
| 11975 | |
| 11976 | // Tests that a PATH_CHALLENGE is received in between other frames in an out of |
| 11977 | // order packet. |
| 11978 | TEST_P(QuicConnectionTest, PathChallengeWithDataInOutOfOrderPacket) { |
| 11979 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 11980 | return; |
| 11981 | } |
| 11982 | PathProbeTestInit(Perspective::IS_SERVER); |
| 11983 | |
| 11984 | QuicFrames frames; |
| 11985 | frames.push_back(QuicFrame(frame1_)); |
| 11986 | QuicPathFrameBuffer path_frame_buffer{0, 1, 2, 3, 4, 5, 6, 7}; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 11987 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 11988 | frames.push_back(QuicFrame(frame2_)); |
| 11989 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback6(), |
| 11990 | /*port=*/23456); |
| 11991 | |
| 11992 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0u); |
| 11993 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 11994 | .Times(2) |
| 11995 | .WillRepeatedly(Invoke([=](const QuicStreamFrame& frame) { |
| 11996 | // Send some data on the stream. The STREAM_FRAME should be built into |
| 11997 | // one packet together with the latter PATH_RESPONSE. |
| 11998 | const std::string data{"response body"}; |
| 11999 | connection_.producer()->SaveStreamData(frame.stream_id, data); |
| 12000 | return notifier_.WriteOrBufferData(frame.stream_id, data.length(), |
| 12001 | NO_FIN); |
| 12002 | })); |
| 12003 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 12004 | .WillOnce(Invoke([=]() { |
| 12005 | // Verify that this packet contains a STREAM_FRAME and is sent to the |
| 12006 | // original peer address. |
| 12007 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 12008 | // No connection migration should happen because the packet is received |
| 12009 | // out of order. |
| 12010 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 12011 | })) |
| 12012 | .WillOnce(Invoke([=]() { |
| 12013 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 12014 | // The final check is to ensure that the random data in the response |
| 12015 | // matches the random data from the challenge. |
| 12016 | EXPECT_EQ(0, |
| 12017 | memcmp(path_frame_buffer.data(), |
| 12018 | &(writer_->path_response_frames().front().data_buffer), |
| 12019 | sizeof(path_frame_buffer))); |
| 12020 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 12021 | // PATH_RESPONSE should be sent in another packet to a different peer |
| 12022 | // address. |
| 12023 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 12024 | })) |
| 12025 | .WillOnce(Invoke([=]() { |
| 12026 | // Verify that this packet contains a STREAM_FRAME and is sent to the |
| 12027 | // original peer address. |
| 12028 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 12029 | // No connection migration should happen because the packet is received |
| 12030 | // out of order. |
| 12031 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 12032 | })); |
| 12033 | // Lower the packet number so that receiving this packet shouldn't trigger |
| 12034 | // peer migration. |
| 12035 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 1); |
| 12036 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 12037 | ENCRYPTION_FORWARD_SECURE); |
| 12038 | } |
| 12039 | |
| 12040 | // Tests that a PATH_CHALLENGE is cached if its PATH_RESPONSE can't be sent. |
| 12041 | TEST_P(QuicConnectionTest, FailToWritePathResponse) { |
| 12042 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
| 12043 | return; |
| 12044 | } |
| 12045 | PathProbeTestInit(Perspective::IS_SERVER); |
| 12046 | |
| 12047 | QuicFrames frames; |
| 12048 | QuicPathFrameBuffer path_frame_buffer{0, 1, 2, 3, 4, 5, 6, 7}; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 12049 | frames.push_back(QuicFrame(QuicPathChallengeFrame(0, path_frame_buffer))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12050 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback6(), |
| 12051 | /*port=*/23456); |
| 12052 | |
| 12053 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0u); |
| 12054 | // Lower the packet number so that receiving this packet shouldn't trigger |
| 12055 | // peer migration. |
| 12056 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 1); |
| 12057 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 12058 | writer_->SetWriteBlocked(); |
| 12059 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress, |
| 12060 | ENCRYPTION_FORWARD_SECURE); |
| 12061 | } |
| 12062 | |
| 12063 | // Regression test for b/168101557. |
| 12064 | TEST_P(QuicConnectionTest, HandshakeDataDoesNotGetPtoed) { |
| 12065 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 12066 | return; |
| 12067 | } |
| 12068 | set_perspective(Perspective::IS_SERVER); |
| 12069 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 12070 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 12071 | } |
| 12072 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12073 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 12074 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 12075 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12076 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 12077 | // Send INITIAL 1. |
| 12078 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 12079 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12080 | connection_.SetEncrypter( |
| 12081 | ENCRYPTION_HANDSHAKE, |
| 12082 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12083 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12084 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12085 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12086 | // Send HANDSHAKE packets. |
| 12087 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 12088 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 12089 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12090 | connection_.SetEncrypter( |
| 12091 | ENCRYPTION_FORWARD_SECURE, |
| 12092 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12093 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12094 | // Send half RTT packet. |
| 12095 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 12096 | |
| 12097 | // Receives HANDSHAKE 1. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12098 | peer_framer_.SetEncrypter( |
| 12099 | ENCRYPTION_HANDSHAKE, |
| 12100 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12101 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_HANDSHAKE); |
| 12102 | // Discard INITIAL key. |
| 12103 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12104 | connection_.NeuterUnencryptedPackets(); |
| 12105 | // Verify there is pending ACK. |
| 12106 | ASSERT_TRUE(connection_.HasPendingAcks()); |
| 12107 | // Set the send alarm. |
| 12108 | connection_.GetSendAlarm()->Set(clock_.ApproximateNow()); |
| 12109 | |
| 12110 | // Fire ACK alarm. |
| 12111 | connection_.GetAckAlarm()->Fire(); |
| 12112 | // Verify 1-RTT packet is coalesced with handshake packet. |
| 12113 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
| 12114 | connection_.GetSendAlarm()->Fire(); |
| 12115 | |
| 12116 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 12117 | connection_.GetRetransmissionAlarm()->Fire(); |
| 12118 | // Verify a handshake packet gets PTOed and 1-RTT packet gets coalesced. |
| 12119 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
| 12120 | } |
| 12121 | |
| 12122 | // Regression test for b/168294218. |
| 12123 | TEST_P(QuicConnectionTest, CoalescerHandlesInitialKeyDiscard) { |
| 12124 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 12125 | return; |
| 12126 | } |
| 12127 | SetQuicReloadableFlag(quic_discard_initial_packet_with_key_dropped, true); |
| 12128 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 12129 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).WillOnce(Invoke([this]() { |
| 12130 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12131 | connection_.NeuterUnencryptedPackets(); |
| 12132 | })); |
| 12133 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 12134 | |
| 12135 | EXPECT_EQ(0u, connection_.GetStats().packets_discarded); |
| 12136 | { |
| 12137 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12138 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 12139 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12140 | connection_.SetEncrypter( |
| 12141 | ENCRYPTION_HANDSHAKE, |
| 12142 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12143 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12144 | connection_.SendCryptoDataWithString(std::string(1200, 'a'), 0); |
| 12145 | // Verify this packet is on hold. |
| 12146 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 12147 | } |
| 12148 | EXPECT_TRUE(connection_.connected()); |
| 12149 | } |
| 12150 | |
| 12151 | // Regresstion test for b/168294218 |
| 12152 | TEST_P(QuicConnectionTest, ZeroRttRejectionAndMissingInitialKeys) { |
| 12153 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 12154 | return; |
| 12155 | } |
| 12156 | // Not defer send in response to packet. |
| 12157 | connection_.set_defer_send_in_response_to_packets(false); |
| 12158 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).WillOnce(Invoke([this]() { |
| 12159 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12160 | connection_.NeuterUnencryptedPackets(); |
| 12161 | })); |
| 12162 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 12163 | .WillRepeatedly(Invoke([=](const QuicCryptoFrame& frame) { |
| 12164 | if (frame.level == ENCRYPTION_HANDSHAKE) { |
| 12165 | // 0-RTT gets rejected. |
| 12166 | connection_.MarkZeroRttPacketsForRetransmission(0); |
| 12167 | // Send Crypto data. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12168 | connection_.SetEncrypter( |
| 12169 | ENCRYPTION_HANDSHAKE, |
| 12170 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12171 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12172 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12173 | connection_.SetEncrypter( |
| 12174 | ENCRYPTION_FORWARD_SECURE, |
| 12175 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12176 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12177 | // Advance INITIAL ack delay to trigger initial ACK to be sent AFTER |
| 12178 | // the retransmission of rejected 0-RTT packets while the HANDSHAKE |
| 12179 | // packet is still in the coalescer, such that the INITIAL key gets |
| 12180 | // dropped between SendAllPendingAcks and actually send the ack frame, |
| 12181 | // bummer. |
| 12182 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
| 12183 | } |
| 12184 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12185 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 12186 | // Send 0-RTT packet. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12187 | connection_.SetEncrypter( |
| 12188 | ENCRYPTION_ZERO_RTT, |
| 12189 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12190 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 12191 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 12192 | |
| 12193 | QuicAckFrame frame1 = InitAckFrame(1); |
| 12194 | // Received ACK for packet 1. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12195 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12196 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 12197 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 12198 | |
| 12199 | // Fire retransmission alarm. |
| 12200 | connection_.GetRetransmissionAlarm()->Fire(); |
| 12201 | |
| 12202 | QuicFrames frames1; |
| 12203 | frames1.push_back(QuicFrame(&crypto_frame_)); |
| 12204 | QuicFrames frames2; |
| 12205 | QuicCryptoFrame crypto_frame(ENCRYPTION_HANDSHAKE, 0, |
| 12206 | absl::string_view(data1)); |
| 12207 | frames2.push_back(QuicFrame(&crypto_frame)); |
| 12208 | ProcessCoalescedPacket( |
| 12209 | {{2, frames1, ENCRYPTION_INITIAL}, {3, frames2, ENCRYPTION_HANDSHAKE}}); |
| 12210 | } |
| 12211 | |
| 12212 | TEST_P(QuicConnectionTest, OnZeroRttPacketAcked) { |
| 12213 | if (!connection_.version().UsesTls()) { |
| 12214 | return; |
| 12215 | } |
| 12216 | MockQuicConnectionDebugVisitor debug_visitor; |
| 12217 | connection_.set_debug_visitor(&debug_visitor); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12218 | connection_.SendCryptoStreamData(); |
| 12219 | // Send 0-RTT packet. |
| 12220 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 12221 | std::make_unique<TaggingEncrypter>(0x02)); |
| 12222 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 12223 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 12224 | connection_.SendStreamDataWithString(4, "bar", 0, NO_FIN); |
| 12225 | // Received ACK for packet 1, HANDSHAKE packet and 1-RTT ACK. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12226 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12227 | .Times(AnyNumber()); |
| 12228 | QuicFrames frames1; |
| 12229 | QuicAckFrame ack_frame1 = InitAckFrame(1); |
| 12230 | frames1.push_back(QuicFrame(&ack_frame1)); |
| 12231 | |
| 12232 | QuicFrames frames2; |
| 12233 | QuicCryptoFrame crypto_frame(ENCRYPTION_HANDSHAKE, 0, |
| 12234 | absl::string_view(data1)); |
| 12235 | frames2.push_back(QuicFrame(&crypto_frame)); |
| 12236 | EXPECT_CALL(debug_visitor, OnZeroRttPacketAcked()).Times(0); |
| 12237 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 12238 | ProcessCoalescedPacket( |
| 12239 | {{1, frames1, ENCRYPTION_INITIAL}, {2, frames2, ENCRYPTION_HANDSHAKE}}); |
| 12240 | |
| 12241 | QuicFrames frames3; |
| 12242 | QuicAckFrame ack_frame2 = |
| 12243 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 12244 | frames3.push_back(QuicFrame(&ack_frame2)); |
| 12245 | EXPECT_CALL(debug_visitor, OnZeroRttPacketAcked()).Times(1); |
| 12246 | ProcessCoalescedPacket({{3, frames3, ENCRYPTION_FORWARD_SECURE}}); |
| 12247 | |
| 12248 | QuicFrames frames4; |
| 12249 | QuicAckFrame ack_frame3 = |
| 12250 | InitAckFrame({{QuicPacketNumber(3), QuicPacketNumber(4)}}); |
| 12251 | frames4.push_back(QuicFrame(&ack_frame3)); |
| 12252 | EXPECT_CALL(debug_visitor, OnZeroRttPacketAcked()).Times(0); |
| 12253 | ProcessCoalescedPacket({{4, frames4, ENCRYPTION_FORWARD_SECURE}}); |
| 12254 | } |
| 12255 | |
| 12256 | TEST_P(QuicConnectionTest, InitiateKeyUpdate) { |
| 12257 | if (!connection_.version().UsesTls()) { |
| 12258 | return; |
| 12259 | } |
| 12260 | |
| 12261 | TransportParameters params; |
| 12262 | QuicConfig config; |
| 12263 | std::string error_details; |
| 12264 | EXPECT_THAT(config.ProcessTransportParameters( |
| 12265 | params, /* is_resumption = */ false, &error_details), |
| 12266 | IsQuicNoError()); |
| 12267 | QuicConfigPeer::SetNegotiated(&config, true); |
| 12268 | if (connection_.version().UsesTls()) { |
| 12269 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 12270 | &config, connection_.connection_id()); |
| 12271 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 12272 | &config, connection_.connection_id()); |
| 12273 | } |
| 12274 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12275 | connection_.SetFromConfig(config); |
| 12276 | |
| 12277 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12278 | |
| 12279 | MockFramerVisitor peer_framer_visitor_; |
| 12280 | peer_framer_.set_visitor(&peer_framer_visitor_); |
| 12281 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12282 | uint8_t correct_tag = ENCRYPTION_FORWARD_SECURE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12283 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12284 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12285 | std::make_unique<TaggingEncrypter>(correct_tag)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12286 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12287 | std::make_unique<StrictTaggingDecrypter>(correct_tag)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12288 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12289 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12290 | connection_.OnHandshakeComplete(); |
| 12291 | |
| 12292 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12293 | std::make_unique<TaggingEncrypter>(correct_tag)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12294 | |
| 12295 | // Key update should still not be allowed, since no packet has been acked |
| 12296 | // from the current key phase. |
| 12297 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12298 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12299 | |
| 12300 | // Send packet 1. |
| 12301 | QuicPacketNumber last_packet; |
| 12302 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 12303 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 12304 | |
| 12305 | // Key update should still not be allowed, even though a packet was sent in |
| 12306 | // the current key phase it hasn't been acked yet. |
| 12307 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12308 | EXPECT_TRUE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12309 | |
| 12310 | EXPECT_FALSE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12311 | // Receive ack for packet 1. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12312 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12313 | QuicAckFrame frame1 = InitAckFrame(1); |
| 12314 | ProcessAckPacket(&frame1); |
| 12315 | |
| 12316 | // OnDecryptedFirstPacketInKeyPhase is called even on the first key phase, |
| 12317 | // so discard_previous_keys_alarm_ should be set now. |
| 12318 | EXPECT_TRUE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12319 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12320 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12321 | correct_tag++; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12322 | // Key update should now be allowed. |
| 12323 | EXPECT_CALL(visitor_, AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12324 | .WillOnce([&correct_tag]() { |
| 12325 | return std::make_unique<StrictTaggingDecrypter>(correct_tag); |
| 12326 | }); |
| 12327 | EXPECT_CALL(visitor_, CreateCurrentOneRttEncrypter()) |
| 12328 | .WillOnce([&correct_tag]() { |
| 12329 | return std::make_unique<TaggingEncrypter>(correct_tag); |
| 12330 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12331 | EXPECT_CALL(visitor_, OnKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12332 | EXPECT_TRUE(connection_.InitiateKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12333 | // discard_previous_keys_alarm_ should not be set until a packet from the new |
| 12334 | // key phase has been received. (The alarm that was set above should be |
| 12335 | // cleared if it hasn't fired before the next key update happened.) |
| 12336 | EXPECT_FALSE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12337 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12338 | |
| 12339 | // Pretend that peer accepts the key update. |
| 12340 | EXPECT_CALL(peer_framer_visitor_, |
| 12341 | AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12342 | .WillOnce([&correct_tag]() { |
| 12343 | return std::make_unique<StrictTaggingDecrypter>(correct_tag); |
| 12344 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12345 | EXPECT_CALL(peer_framer_visitor_, CreateCurrentOneRttEncrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12346 | .WillOnce([&correct_tag]() { |
| 12347 | return std::make_unique<TaggingEncrypter>(correct_tag); |
| 12348 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12349 | peer_framer_.SetKeyUpdateSupportForConnection(true); |
| 12350 | peer_framer_.DoKeyUpdate(KeyUpdateReason::kRemote); |
| 12351 | |
| 12352 | // Another key update should not be allowed yet. |
| 12353 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12354 | |
| 12355 | // Send packet 2. |
| 12356 | SendStreamDataToPeer(2, "bar", 0, NO_FIN, &last_packet); |
| 12357 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 12358 | EXPECT_TRUE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12359 | // Receive ack for packet 2. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12360 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12361 | QuicAckFrame frame2 = InitAckFrame(2); |
| 12362 | ProcessAckPacket(&frame2); |
| 12363 | EXPECT_TRUE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12364 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12365 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12366 | correct_tag++; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12367 | // Key update should be allowed again now that a packet has been acked from |
| 12368 | // the current key phase. |
| 12369 | EXPECT_CALL(visitor_, AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12370 | .WillOnce([&correct_tag]() { |
| 12371 | return std::make_unique<StrictTaggingDecrypter>(correct_tag); |
| 12372 | }); |
| 12373 | EXPECT_CALL(visitor_, CreateCurrentOneRttEncrypter()) |
| 12374 | .WillOnce([&correct_tag]() { |
| 12375 | return std::make_unique<TaggingEncrypter>(correct_tag); |
| 12376 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12377 | EXPECT_CALL(visitor_, OnKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12378 | EXPECT_TRUE(connection_.InitiateKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12379 | |
| 12380 | // Pretend that peer accepts the key update. |
| 12381 | EXPECT_CALL(peer_framer_visitor_, |
| 12382 | AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12383 | .WillOnce([&correct_tag]() { |
| 12384 | return std::make_unique<StrictTaggingDecrypter>(correct_tag); |
| 12385 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12386 | EXPECT_CALL(peer_framer_visitor_, CreateCurrentOneRttEncrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12387 | .WillOnce([&correct_tag]() { |
| 12388 | return std::make_unique<TaggingEncrypter>(correct_tag); |
| 12389 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12390 | peer_framer_.DoKeyUpdate(KeyUpdateReason::kRemote); |
| 12391 | |
| 12392 | // Another key update should not be allowed yet. |
| 12393 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12394 | |
| 12395 | // Send packet 3. |
| 12396 | SendStreamDataToPeer(3, "baz", 0, NO_FIN, &last_packet); |
| 12397 | EXPECT_EQ(QuicPacketNumber(3u), last_packet); |
| 12398 | |
| 12399 | // Another key update should not be allowed yet. |
| 12400 | EXPECT_FALSE(connection_.IsKeyUpdateAllowed()); |
| 12401 | EXPECT_TRUE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12402 | |
| 12403 | // Receive ack for packet 3. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12404 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12405 | QuicAckFrame frame3 = InitAckFrame(3); |
| 12406 | ProcessAckPacket(&frame3); |
| 12407 | EXPECT_TRUE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12408 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12409 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12410 | correct_tag++; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12411 | // Key update should be allowed now. |
| 12412 | EXPECT_CALL(visitor_, AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12413 | .WillOnce([&correct_tag]() { |
| 12414 | return std::make_unique<StrictTaggingDecrypter>(correct_tag); |
| 12415 | }); |
| 12416 | EXPECT_CALL(visitor_, CreateCurrentOneRttEncrypter()) |
| 12417 | .WillOnce([&correct_tag]() { |
| 12418 | return std::make_unique<TaggingEncrypter>(correct_tag); |
| 12419 | }); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12420 | EXPECT_CALL(visitor_, OnKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12421 | EXPECT_TRUE(connection_.InitiateKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12422 | EXPECT_FALSE(connection_.GetDiscardPreviousOneRttKeysAlarm()->IsSet()); |
| 12423 | EXPECT_FALSE(connection_.HaveSentPacketsInCurrentKeyPhaseButNoneAcked()); |
| 12424 | } |
| 12425 | |
| 12426 | TEST_P(QuicConnectionTest, InitiateKeyUpdateApproachingConfidentialityLimit) { |
| 12427 | if (!connection_.version().UsesTls()) { |
| 12428 | return; |
| 12429 | } |
| 12430 | |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 12431 | SetQuicFlag(quic_key_update_confidentiality_limit, 3U); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12432 | |
| 12433 | std::string error_details; |
| 12434 | TransportParameters params; |
| 12435 | // Key update is enabled. |
| 12436 | QuicConfig config; |
| 12437 | EXPECT_THAT(config.ProcessTransportParameters( |
| 12438 | params, /* is_resumption = */ false, &error_details), |
| 12439 | IsQuicNoError()); |
| 12440 | QuicConfigPeer::SetNegotiated(&config, true); |
| 12441 | if (connection_.version().UsesTls()) { |
| 12442 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 12443 | &config, connection_.connection_id()); |
| 12444 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 12445 | &config, connection_.connection_id()); |
| 12446 | } |
| 12447 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12448 | connection_.SetFromConfig(config); |
| 12449 | |
| 12450 | MockFramerVisitor peer_framer_visitor_; |
| 12451 | peer_framer_.set_visitor(&peer_framer_visitor_); |
| 12452 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12453 | uint8_t current_tag = ENCRYPTION_FORWARD_SECURE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12454 | |
| 12455 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12456 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12457 | std::make_unique<TaggingEncrypter>(current_tag)); |
| 12458 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
| 12459 | std::make_unique<StrictTaggingDecrypter>(current_tag)); |
| 12460 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12461 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12462 | connection_.OnHandshakeComplete(); |
| 12463 | |
| 12464 | peer_framer_.SetKeyUpdateSupportForConnection(true); |
| 12465 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12466 | std::make_unique<TaggingEncrypter>(current_tag)); |
| 12467 | |
| 12468 | const QuicConnectionStats& stats = connection_.GetStats(); |
| 12469 | |
| 12470 | for (int packet_num = 1; packet_num <= 8; ++packet_num) { |
| 12471 | if (packet_num == 3 || packet_num == 6) { |
| 12472 | current_tag++; |
| 12473 | EXPECT_CALL(visitor_, AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
| 12474 | .WillOnce([current_tag]() { |
| 12475 | return std::make_unique<StrictTaggingDecrypter>(current_tag); |
| 12476 | }); |
| 12477 | EXPECT_CALL(visitor_, CreateCurrentOneRttEncrypter()) |
| 12478 | .WillOnce([current_tag]() { |
| 12479 | return std::make_unique<TaggingEncrypter>(current_tag); |
| 12480 | }); |
| 12481 | EXPECT_CALL(visitor_, |
| 12482 | OnKeyUpdate(KeyUpdateReason::kLocalKeyUpdateLimitOverride)); |
| 12483 | } |
| 12484 | // Send packet. |
| 12485 | QuicPacketNumber last_packet; |
| 12486 | SendStreamDataToPeer(packet_num, "foo", 0, NO_FIN, &last_packet); |
| 12487 | EXPECT_EQ(QuicPacketNumber(packet_num), last_packet); |
| 12488 | if (packet_num >= 6) { |
| 12489 | EXPECT_EQ(2U, stats.key_update_count); |
| 12490 | } else if (packet_num >= 3) { |
| 12491 | EXPECT_EQ(1U, stats.key_update_count); |
| 12492 | } else { |
| 12493 | EXPECT_EQ(0U, stats.key_update_count); |
| 12494 | } |
| 12495 | |
| 12496 | if (packet_num == 4 || packet_num == 7) { |
| 12497 | // Pretend that peer accepts the key update. |
| 12498 | EXPECT_CALL(peer_framer_visitor_, |
| 12499 | AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
| 12500 | .WillOnce([current_tag]() { |
| 12501 | return std::make_unique<StrictTaggingDecrypter>(current_tag); |
| 12502 | }); |
| 12503 | EXPECT_CALL(peer_framer_visitor_, CreateCurrentOneRttEncrypter()) |
| 12504 | .WillOnce([current_tag]() { |
| 12505 | return std::make_unique<TaggingEncrypter>(current_tag); |
| 12506 | }); |
| 12507 | peer_framer_.DoKeyUpdate(KeyUpdateReason::kRemote); |
| 12508 | } |
| 12509 | // Receive ack for packet. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12510 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12511 | QuicAckFrame frame1 = InitAckFrame(packet_num); |
| 12512 | ProcessAckPacket(&frame1); |
| 12513 | } |
| 12514 | } |
| 12515 | |
| 12516 | TEST_P(QuicConnectionTest, |
| 12517 | CloseConnectionOnConfidentialityLimitKeyUpdateNotAllowed) { |
| 12518 | if (!connection_.version().UsesTls()) { |
| 12519 | return; |
| 12520 | } |
| 12521 | |
| 12522 | // Set key update confidentiality limit to 1 packet. |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 12523 | SetQuicFlag(quic_key_update_confidentiality_limit, 1U); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12524 | // Use confidentiality limit for connection close of 3 packets. |
| 12525 | constexpr size_t kConfidentialityLimit = 3U; |
| 12526 | |
| 12527 | std::string error_details; |
| 12528 | TransportParameters params; |
| 12529 | // Key update is enabled. |
| 12530 | QuicConfig config; |
| 12531 | EXPECT_THAT(config.ProcessTransportParameters( |
| 12532 | params, /* is_resumption = */ false, &error_details), |
| 12533 | IsQuicNoError()); |
| 12534 | QuicConfigPeer::SetNegotiated(&config, true); |
| 12535 | if (connection_.version().UsesTls()) { |
| 12536 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 12537 | &config, connection_.connection_id()); |
| 12538 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 12539 | &config, connection_.connection_id()); |
| 12540 | } |
| 12541 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12542 | connection_.SetFromConfig(config); |
| 12543 | |
| 12544 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12545 | connection_.SetEncrypter( |
| 12546 | ENCRYPTION_FORWARD_SECURE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12547 | std::make_unique<TaggingEncrypterWithConfidentialityLimit>( |
| 12548 | ENCRYPTION_FORWARD_SECURE, kConfidentialityLimit)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12549 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12550 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12551 | connection_.OnHandshakeComplete(); |
| 12552 | |
| 12553 | QuicPacketNumber last_packet; |
| 12554 | // Send 3 packets without receiving acks for any of them. Key update will not |
| 12555 | // be allowed, so the confidentiality limit should be reached, forcing the |
| 12556 | // connection to be closed. |
| 12557 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 12558 | EXPECT_TRUE(connection_.connected()); |
| 12559 | SendStreamDataToPeer(2, "foo", 0, NO_FIN, &last_packet); |
| 12560 | EXPECT_TRUE(connection_.connected()); |
| 12561 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 12562 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); |
| 12563 | EXPECT_FALSE(connection_.connected()); |
| 12564 | const QuicConnectionStats& stats = connection_.GetStats(); |
| 12565 | EXPECT_EQ(0U, stats.key_update_count); |
| 12566 | TestConnectionCloseQuicErrorCode(QUIC_AEAD_LIMIT_REACHED); |
| 12567 | } |
| 12568 | |
| 12569 | TEST_P(QuicConnectionTest, CloseConnectionOnIntegrityLimitDuringHandshake) { |
| 12570 | if (!connection_.version().UsesTls()) { |
| 12571 | return; |
| 12572 | } |
| 12573 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12574 | constexpr uint8_t correct_tag = ENCRYPTION_HANDSHAKE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12575 | constexpr uint8_t wrong_tag = 0xFE; |
| 12576 | constexpr QuicPacketCount kIntegrityLimit = 3; |
| 12577 | |
| 12578 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
| 12579 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12580 | correct_tag, kIntegrityLimit)); |
| 12581 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12582 | std::make_unique<TaggingEncrypter>(correct_tag)); |
| 12583 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12584 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12585 | std::make_unique<TaggingEncrypter>(wrong_tag)); |
| 12586 | for (uint64_t i = 1; i <= kIntegrityLimit; ++i) { |
| 12587 | EXPECT_TRUE(connection_.connected()); |
| 12588 | if (i == kIntegrityLimit) { |
| 12589 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 12590 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(AnyNumber()); |
| 12591 | } |
| 12592 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 12593 | EXPECT_EQ( |
| 12594 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12595 | } |
| 12596 | EXPECT_FALSE(connection_.connected()); |
| 12597 | TestConnectionCloseQuicErrorCode(QUIC_AEAD_LIMIT_REACHED); |
| 12598 | } |
| 12599 | |
| 12600 | TEST_P(QuicConnectionTest, CloseConnectionOnIntegrityLimitAfterHandshake) { |
| 12601 | if (!connection_.version().UsesTls()) { |
| 12602 | return; |
| 12603 | } |
| 12604 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12605 | constexpr uint8_t correct_tag = ENCRYPTION_FORWARD_SECURE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12606 | constexpr uint8_t wrong_tag = 0xFE; |
| 12607 | constexpr QuicPacketCount kIntegrityLimit = 3; |
| 12608 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12609 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
| 12610 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12611 | correct_tag, kIntegrityLimit)); |
| 12612 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12613 | std::make_unique<TaggingEncrypter>(correct_tag)); |
| 12614 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12615 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12616 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12617 | connection_.OnHandshakeComplete(); |
| 12618 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12619 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12620 | std::make_unique<TaggingEncrypter>(wrong_tag)); |
| 12621 | for (uint64_t i = 1; i <= kIntegrityLimit; ++i) { |
| 12622 | EXPECT_TRUE(connection_.connected()); |
| 12623 | if (i == kIntegrityLimit) { |
| 12624 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 12625 | } |
| 12626 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 12627 | EXPECT_EQ( |
| 12628 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12629 | } |
| 12630 | EXPECT_FALSE(connection_.connected()); |
| 12631 | TestConnectionCloseQuicErrorCode(QUIC_AEAD_LIMIT_REACHED); |
| 12632 | } |
| 12633 | |
| 12634 | TEST_P(QuicConnectionTest, |
| 12635 | CloseConnectionOnIntegrityLimitAcrossEncryptionLevels) { |
| 12636 | if (!connection_.version().UsesTls()) { |
| 12637 | return; |
| 12638 | } |
| 12639 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12640 | uint8_t correct_tag = ENCRYPTION_HANDSHAKE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12641 | constexpr uint8_t wrong_tag = 0xFE; |
| 12642 | constexpr QuicPacketCount kIntegrityLimit = 4; |
| 12643 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12644 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
| 12645 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12646 | correct_tag, kIntegrityLimit)); |
| 12647 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12648 | std::make_unique<TaggingEncrypter>(correct_tag)); |
| 12649 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12650 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12651 | std::make_unique<TaggingEncrypter>(wrong_tag)); |
| 12652 | for (uint64_t i = 1; i <= 2; ++i) { |
| 12653 | EXPECT_TRUE(connection_.connected()); |
| 12654 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 12655 | EXPECT_EQ( |
| 12656 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12657 | } |
| 12658 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12659 | correct_tag = ENCRYPTION_FORWARD_SECURE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12660 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
| 12661 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12662 | correct_tag, kIntegrityLimit)); |
| 12663 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12664 | std::make_unique<TaggingEncrypter>(correct_tag)); |
| 12665 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12666 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12667 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12668 | connection_.OnHandshakeComplete(); |
| 12669 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12670 | connection_.RemoveEncrypter(ENCRYPTION_HANDSHAKE); |
| 12671 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12672 | std::make_unique<TaggingEncrypter>(wrong_tag)); |
| 12673 | for (uint64_t i = 3; i <= kIntegrityLimit; ++i) { |
| 12674 | EXPECT_TRUE(connection_.connected()); |
| 12675 | if (i == kIntegrityLimit) { |
| 12676 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 12677 | } |
| 12678 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 12679 | EXPECT_EQ( |
| 12680 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12681 | } |
| 12682 | EXPECT_FALSE(connection_.connected()); |
| 12683 | TestConnectionCloseQuicErrorCode(QUIC_AEAD_LIMIT_REACHED); |
| 12684 | } |
| 12685 | |
| 12686 | TEST_P(QuicConnectionTest, IntegrityLimitDoesNotApplyWithoutDecryptionKey) { |
| 12687 | if (!connection_.version().UsesTls()) { |
| 12688 | return; |
| 12689 | } |
| 12690 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12691 | constexpr uint8_t correct_tag = ENCRYPTION_HANDSHAKE; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12692 | constexpr uint8_t wrong_tag = 0xFE; |
| 12693 | constexpr QuicPacketCount kIntegrityLimit = 3; |
| 12694 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12695 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
| 12696 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12697 | correct_tag, kIntegrityLimit)); |
| 12698 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12699 | std::make_unique<TaggingEncrypter>(correct_tag)); |
| 12700 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12701 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
| 12702 | |
| 12703 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12704 | std::make_unique<TaggingEncrypter>(wrong_tag)); |
| 12705 | for (uint64_t i = 1; i <= kIntegrityLimit * 2; ++i) { |
| 12706 | EXPECT_TRUE(connection_.connected()); |
| 12707 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 12708 | EXPECT_EQ( |
| 12709 | 0u, connection_.GetStats().num_failed_authentication_packets_received); |
| 12710 | } |
| 12711 | EXPECT_TRUE(connection_.connected()); |
| 12712 | } |
| 12713 | |
| 12714 | TEST_P(QuicConnectionTest, CloseConnectionOnIntegrityLimitAcrossKeyPhases) { |
| 12715 | if (!connection_.version().UsesTls()) { |
| 12716 | return; |
| 12717 | } |
| 12718 | |
| 12719 | constexpr QuicPacketCount kIntegrityLimit = 4; |
| 12720 | |
| 12721 | TransportParameters params; |
| 12722 | QuicConfig config; |
| 12723 | std::string error_details; |
| 12724 | EXPECT_THAT(config.ProcessTransportParameters( |
| 12725 | params, /* is_resumption = */ false, &error_details), |
| 12726 | IsQuicNoError()); |
| 12727 | QuicConfigPeer::SetNegotiated(&config, true); |
| 12728 | if (connection_.version().UsesTls()) { |
| 12729 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 12730 | &config, connection_.connection_id()); |
| 12731 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 12732 | &config, connection_.connection_id()); |
| 12733 | } |
| 12734 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12735 | connection_.SetFromConfig(config); |
| 12736 | |
| 12737 | MockFramerVisitor peer_framer_visitor_; |
| 12738 | peer_framer_.set_visitor(&peer_framer_visitor_); |
| 12739 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12740 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12741 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12742 | std::make_unique<TaggingEncrypter>(0x01)); |
| 12743 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
| 12744 | std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12745 | ENCRYPTION_FORWARD_SECURE, kIntegrityLimit)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12746 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12747 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12748 | connection_.OnHandshakeComplete(); |
| 12749 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 12750 | |
| 12751 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12752 | std::make_unique<TaggingEncrypter>(0xFF)); |
| 12753 | for (uint64_t i = 1; i <= 2; ++i) { |
| 12754 | EXPECT_TRUE(connection_.connected()); |
| 12755 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 12756 | EXPECT_EQ( |
| 12757 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12758 | } |
| 12759 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12760 | peer_framer_.SetEncrypter( |
| 12761 | ENCRYPTION_FORWARD_SECURE, |
| 12762 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12763 | // Send packet 1. |
| 12764 | QuicPacketNumber last_packet; |
| 12765 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 12766 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 12767 | // Receive ack for packet 1. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12768 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12769 | QuicAckFrame frame1 = InitAckFrame(1); |
| 12770 | ProcessAckPacket(&frame1); |
| 12771 | // Key update should now be allowed, initiate it. |
| 12772 | EXPECT_CALL(visitor_, AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
| 12773 | .WillOnce([kIntegrityLimit]() { |
| 12774 | return std::make_unique<StrictTaggingDecrypterWithIntegrityLimit>( |
| 12775 | 0x02, kIntegrityLimit); |
| 12776 | }); |
| 12777 | EXPECT_CALL(visitor_, CreateCurrentOneRttEncrypter()).WillOnce([]() { |
| 12778 | return std::make_unique<TaggingEncrypter>(0x02); |
| 12779 | }); |
| 12780 | EXPECT_CALL(visitor_, OnKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12781 | EXPECT_TRUE(connection_.InitiateKeyUpdate(KeyUpdateReason::kLocalForTests)); |
| 12782 | |
| 12783 | // Pretend that peer accepts the key update. |
| 12784 | EXPECT_CALL(peer_framer_visitor_, |
| 12785 | AdvanceKeysAndCreateCurrentOneRttDecrypter()) |
| 12786 | .WillOnce( |
| 12787 | []() { return std::make_unique<StrictTaggingDecrypter>(0x02); }); |
| 12788 | EXPECT_CALL(peer_framer_visitor_, CreateCurrentOneRttEncrypter()) |
| 12789 | .WillOnce([]() { return std::make_unique<TaggingEncrypter>(0x02); }); |
| 12790 | peer_framer_.SetKeyUpdateSupportForConnection(true); |
| 12791 | peer_framer_.DoKeyUpdate(KeyUpdateReason::kLocalForTests); |
| 12792 | |
| 12793 | // Send packet 2. |
| 12794 | SendStreamDataToPeer(2, "bar", 0, NO_FIN, &last_packet); |
| 12795 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 12796 | // Receive ack for packet 2. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12797 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12798 | QuicAckFrame frame2 = InitAckFrame(2); |
| 12799 | ProcessAckPacket(&frame2); |
| 12800 | |
| 12801 | EXPECT_EQ(2u, |
| 12802 | connection_.GetStats().num_failed_authentication_packets_received); |
| 12803 | |
| 12804 | // Do two more undecryptable packets. Integrity limit should be reached. |
| 12805 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 12806 | std::make_unique<TaggingEncrypter>(0xFF)); |
| 12807 | for (uint64_t i = 3; i <= kIntegrityLimit; ++i) { |
| 12808 | EXPECT_TRUE(connection_.connected()); |
| 12809 | if (i == kIntegrityLimit) { |
| 12810 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 12811 | } |
| 12812 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 12813 | EXPECT_EQ( |
| 12814 | i, connection_.GetStats().num_failed_authentication_packets_received); |
| 12815 | } |
| 12816 | EXPECT_FALSE(connection_.connected()); |
| 12817 | TestConnectionCloseQuicErrorCode(QUIC_AEAD_LIMIT_REACHED); |
| 12818 | } |
| 12819 | |
| 12820 | TEST_P(QuicConnectionTest, SendAckFrequencyFrame) { |
| 12821 | if (!version().HasIetfQuicFrames()) { |
| 12822 | return; |
| 12823 | } |
| 12824 | SetQuicReloadableFlag(quic_can_send_ack_frequency, true); |
| 12825 | set_perspective(Perspective::IS_SERVER); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12826 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12827 | .Times(AnyNumber()); |
| 12828 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 12829 | |
| 12830 | QuicConfig config; |
| 12831 | QuicConfigPeer::SetReceivedMinAckDelayMs(&config, /*min_ack_delay_ms=*/1); |
| 12832 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12833 | connection_.SetFromConfig(config); |
| 12834 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 12835 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12836 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 12837 | |
| 12838 | connection_.OnHandshakeComplete(); |
| 12839 | |
| 12840 | writer_->SetWritable(); |
| 12841 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 99); |
| 12842 | // Send packet 100 |
| 12843 | SendStreamDataToPeer(/*id=*/1, "foo", /*offset=*/0, NO_FIN, nullptr); |
| 12844 | |
| 12845 | QuicAckFrequencyFrame captured_frame; |
| 12846 | EXPECT_CALL(visitor_, SendAckFrequency(_)) |
| 12847 | .WillOnce(Invoke([&captured_frame](const QuicAckFrequencyFrame& frame) { |
| 12848 | captured_frame = frame; |
| 12849 | })); |
| 12850 | // Send packet 101. |
| 12851 | SendStreamDataToPeer(/*id=*/1, "bar", /*offset=*/3, NO_FIN, nullptr); |
| 12852 | |
| 12853 | EXPECT_EQ(captured_frame.packet_tolerance, 10u); |
| 12854 | EXPECT_EQ(captured_frame.max_ack_delay, |
| 12855 | QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs)); |
| 12856 | |
| 12857 | // Sending packet 102 does not trigger sending another AckFrequencyFrame. |
| 12858 | SendStreamDataToPeer(/*id=*/1, "baz", /*offset=*/6, NO_FIN, nullptr); |
| 12859 | } |
| 12860 | |
| 12861 | TEST_P(QuicConnectionTest, SendAckFrequencyFrameUponHandshakeCompletion) { |
| 12862 | if (!version().HasIetfQuicFrames()) { |
| 12863 | return; |
| 12864 | } |
| 12865 | SetQuicReloadableFlag(quic_can_send_ack_frequency, true); |
| 12866 | set_perspective(Perspective::IS_SERVER); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 12867 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12868 | .Times(AnyNumber()); |
| 12869 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 12870 | |
| 12871 | QuicConfig config; |
| 12872 | QuicConfigPeer::SetReceivedMinAckDelayMs(&config, /*min_ack_delay_ms=*/1); |
| 12873 | QuicTagVector quic_tag_vector; |
| 12874 | // Enable sending AckFrequency upon handshake completion. |
| 12875 | quic_tag_vector.push_back(kAFF2); |
| 12876 | QuicConfigPeer::SetReceivedConnectionOptions(&config, quic_tag_vector); |
| 12877 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12878 | connection_.SetFromConfig(config); |
| 12879 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 12880 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 12881 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 12882 | |
| 12883 | QuicAckFrequencyFrame captured_frame; |
| 12884 | EXPECT_CALL(visitor_, SendAckFrequency(_)) |
| 12885 | .WillOnce(Invoke([&captured_frame](const QuicAckFrequencyFrame& frame) { |
| 12886 | captured_frame = frame; |
| 12887 | })); |
| 12888 | |
| 12889 | connection_.OnHandshakeComplete(); |
| 12890 | |
| 12891 | EXPECT_EQ(captured_frame.packet_tolerance, 2u); |
| 12892 | EXPECT_EQ(captured_frame.max_ack_delay, |
| 12893 | QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs)); |
| 12894 | } |
| 12895 | |
| 12896 | TEST_P(QuicConnectionTest, FastRecoveryOfLostServerHello) { |
| 12897 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 12898 | return; |
| 12899 | } |
| 12900 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12901 | QuicConfig config; |
| 12902 | connection_.SetFromConfig(config); |
| 12903 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12904 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 12905 | connection_.SendCryptoStreamData(); |
| 12906 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 12907 | |
| 12908 | // Assume ServerHello gets lost. |
| 12909 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12910 | std::make_unique<TaggingEncrypter>(0x02)); |
| 12911 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_HANDSHAKE); |
| 12912 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 12913 | // Shorten PTO for fast recovery from lost ServerHello. |
| 12914 | EXPECT_EQ(clock_.ApproximateNow() + kAlarmGranularity, |
| 12915 | connection_.GetRetransmissionAlarm()->deadline()); |
| 12916 | } |
| 12917 | |
| 12918 | TEST_P(QuicConnectionTest, ServerHelloGetsReordered) { |
| 12919 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 12920 | return; |
| 12921 | } |
| 12922 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 12923 | QuicConfig config; |
| 12924 | connection_.SetFromConfig(config); |
| 12925 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 12926 | .WillRepeatedly(Invoke([=](const QuicCryptoFrame& frame) { |
| 12927 | if (frame.level == ENCRYPTION_INITIAL) { |
| 12928 | // Install handshake read keys. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 12929 | SetDecrypter( |
| 12930 | ENCRYPTION_HANDSHAKE, |
| 12931 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
| 12932 | connection_.SetEncrypter( |
| 12933 | ENCRYPTION_HANDSHAKE, |
| 12934 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12935 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 12936 | } |
| 12937 | })); |
| 12938 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12939 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 12940 | connection_.SendCryptoStreamData(); |
| 12941 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 12942 | |
| 12943 | // Assume ServerHello gets reordered. |
| 12944 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 12945 | std::make_unique<TaggingEncrypter>(0x02)); |
| 12946 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_HANDSHAKE); |
| 12947 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 12948 | // Verify fast recovery is not enabled. |
| 12949 | EXPECT_EQ(connection_.sent_packet_manager().GetRetransmissionTime(), |
| 12950 | connection_.GetRetransmissionAlarm()->deadline()); |
| 12951 | } |
| 12952 | |
| 12953 | TEST_P(QuicConnectionTest, MigratePath) { |
| 12954 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 12955 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 12956 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 12957 | connection_.OnPathDegradingDetected(); |
| 12958 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 12959 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 12960 | |
| 12961 | // Buffer a packet. |
| 12962 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 12963 | writer_->SetWriteBlocked(); |
| 12964 | connection_.SendMtuDiscoveryPacket(kMaxOutgoingPacketSize); |
| 12965 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 12966 | |
| 12967 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 12968 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()); |
| 12969 | connection_.MigratePath(kNewSelfAddress, connection_.peer_address(), |
| 12970 | &new_writer, /*owns_writer=*/false); |
| 12971 | |
| 12972 | EXPECT_EQ(kNewSelfAddress, connection_.self_address()); |
| 12973 | EXPECT_EQ(&new_writer, QuicConnectionPeer::GetWriter(&connection_)); |
| 12974 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 12975 | // Buffered packet on the old path should be discarded. |
| 12976 | if (connection_.connection_migration_use_new_cid()) { |
| 12977 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 12978 | } else { |
| 12979 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 12980 | } |
| 12981 | } |
| 12982 | |
| 12983 | TEST_P(QuicConnectionTest, MigrateToNewPathDuringProbing) { |
danzh | 8760571 | 2022-04-11 14:36:39 -0700 | [diff] [blame] | 12984 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12985 | return; |
| 12986 | } |
| 12987 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 12988 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Any4(), 12345); |
| 12989 | EXPECT_NE(kNewSelfAddress, connection_.self_address()); |
| 12990 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 12991 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 12992 | bool success = false; |
| 12993 | connection_.ValidatePath( |
| 12994 | std::make_unique<TestQuicPathValidationContext>( |
| 12995 | kNewSelfAddress, connection_.peer_address(), &new_writer), |
| 12996 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 12997 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 12998 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12999 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13000 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13001 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13002 | |
| 13003 | connection_.MigratePath(kNewSelfAddress, connection_.peer_address(), |
| 13004 | &new_writer, /*owns_writer=*/false); |
| 13005 | EXPECT_EQ(kNewSelfAddress, connection_.self_address()); |
| 13006 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13007 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 13008 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13009 | } |
| 13010 | |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13011 | TEST_P(QuicConnectionTest, MultiPortConnection) { |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13012 | set_perspective(Perspective::IS_CLIENT); |
| 13013 | QuicConfig config; |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13014 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM}); |
| 13015 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13016 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13017 | connection_.SetFromConfig(config); |
| 13018 | if (!connection_.connection_migration_use_new_cid()) { |
| 13019 | return; |
| 13020 | } |
| 13021 | connection_.CreateConnectionIdManager(); |
| 13022 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13023 | connection_.OnHandshakeComplete(); |
| 13024 | |
renjietang | 864fbda | 2022-09-08 10:50:07 -0700 | [diff] [blame] | 13025 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 13026 | connection_.OnPathDegradingDetected(); |
| 13027 | |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13028 | auto self_address = connection_.self_address(); |
| 13029 | const QuicSocketAddress kNewSelfAddress(self_address.host(), |
| 13030 | self_address.port() + 1); |
| 13031 | EXPECT_NE(kNewSelfAddress, self_address); |
| 13032 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 13033 | |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13034 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()).WillOnce(Return(false)); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13035 | QuicNewConnectionIdFrame frame; |
| 13036 | frame.connection_id = TestConnectionId(1234); |
| 13037 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13038 | frame.stateless_reset_token = |
| 13039 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13040 | frame.retire_prior_to = 0u; |
| 13041 | frame.sequence_number = 1u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13042 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13043 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13044 | observer->OnMultiPortPathContextAvailable( |
| 13045 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13046 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13047 | })); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13048 | connection_.OnNewConnectionIdFrame(frame); |
| 13049 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13050 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13051 | &connection_, kNewSelfAddress, connection_.peer_address())); |
renjietang | 95731fa | 2022-09-26 14:06:32 -0700 | [diff] [blame] | 13052 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13053 | EXPECT_FALSE(alt_path->validated); |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 13054 | EXPECT_EQ(PathValidationReason::kMultiPort, |
| 13055 | QuicConnectionPeer::path_validator(&connection_) |
| 13056 | ->GetPathValidationReason()); |
| 13057 | |
| 13058 | // Suppose the server retransmits the NEW_CID frame, the client will receive |
| 13059 | // the same frame again. It should be ignored. |
| 13060 | // Regression test of crbug.com/1406762 |
| 13061 | connection_.OnNewConnectionIdFrame(frame); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13062 | |
renjietang | 864fbda | 2022-09-08 10:50:07 -0700 | [diff] [blame] | 13063 | // 30ms RTT. |
| 13064 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 13065 | // Fake a response delay. |
| 13066 | clock_.AdvanceTime(kTestRTT); |
| 13067 | |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13068 | QuicFrames frames; |
| 13069 | frames.push_back(QuicFrame(QuicPathResponseFrame( |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13070 | 99, new_writer.path_challenge_frames().back().data_buffer))); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13071 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 13072 | ENCRYPTION_FORWARD_SECURE); |
| 13073 | // No migration should happen and the alternative path should still be alive. |
| 13074 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13075 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13076 | &connection_, kNewSelfAddress, connection_.peer_address())); |
renjietang | 95731fa | 2022-09-26 14:06:32 -0700 | [diff] [blame] | 13077 | EXPECT_TRUE(alt_path->validated); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13078 | |
renjietang | 864fbda | 2022-09-08 10:50:07 -0700 | [diff] [blame] | 13079 | auto stats = connection_.multi_port_stats(); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 13080 | EXPECT_EQ(1, connection_.GetStats().num_path_degrading); |
renjietang | 864fbda | 2022-09-08 10:50:07 -0700 | [diff] [blame] | 13081 | EXPECT_EQ(0, stats->num_multi_port_probe_failures_when_path_degrading); |
| 13082 | EXPECT_EQ(kTestRTT, stats->rtt_stats.latest_rtt()); |
| 13083 | EXPECT_EQ(kTestRTT, |
| 13084 | stats->rtt_stats_when_default_path_degrading.latest_rtt()); |
| 13085 | |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13086 | // When there's no active request, the probing shouldn't happen. But the |
| 13087 | // probing context should be saved. |
| 13088 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()).WillOnce(Return(false)); |
| 13089 | connection_.GetMultiPortProbingAlarm()->Fire(); |
| 13090 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13091 | EXPECT_FALSE(connection_.GetMultiPortProbingAlarm()->IsSet()); |
| 13092 | |
| 13093 | // Simulate the situation where a new request stream is created. |
| 13094 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 13095 | .WillRepeatedly(Return(true)); |
| 13096 | random_generator_.ChangeValue(); |
| 13097 | connection_.MaybeProbeMultiPortPath(); |
| 13098 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13099 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13100 | EXPECT_TRUE(alt_path->validated); |
| 13101 | // Fake a response delay. |
| 13102 | clock_.AdvanceTime(kTestRTT); |
| 13103 | QuicFrames frames2; |
| 13104 | frames2.push_back(QuicFrame(QuicPathResponseFrame( |
| 13105 | 99, new_writer.path_challenge_frames().back().data_buffer))); |
| 13106 | ProcessFramesPacketWithAddresses(frames2, kNewSelfAddress, kPeerAddress, |
| 13107 | ENCRYPTION_FORWARD_SECURE); |
| 13108 | // No migration should happen and the alternative path should still be alive. |
| 13109 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13110 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13111 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13112 | EXPECT_TRUE(alt_path->validated); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 13113 | EXPECT_EQ(1, connection_.GetStats().num_path_degrading); |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13114 | EXPECT_EQ(0, stats->num_multi_port_probe_failures_when_path_degrading); |
| 13115 | EXPECT_EQ(kTestRTT, stats->rtt_stats.latest_rtt()); |
| 13116 | EXPECT_EQ(kTestRTT, |
| 13117 | stats->rtt_stats_when_default_path_degrading.latest_rtt()); |
| 13118 | |
| 13119 | EXPECT_TRUE(connection_.GetMultiPortProbingAlarm()->IsSet()); |
| 13120 | // Since there's already a scheduled probing alarm, manual calls won't have |
| 13121 | // any effect. |
| 13122 | connection_.MaybeProbeMultiPortPath(); |
| 13123 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13124 | |
| 13125 | // Simulate the case where the path validation fails after retries. |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13126 | connection_.GetMultiPortProbingAlarm()->Fire(); |
| 13127 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13128 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13129 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13130 | for (size_t i = 0; i < QuicPathValidator::kMaxRetryTimes + 1; ++i) { |
| 13131 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 13132 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 13133 | QuicPathValidatorPeer::retry_timer( |
| 13134 | QuicConnectionPeer::path_validator(&connection_))) |
| 13135 | ->Fire(); |
| 13136 | } |
| 13137 | |
| 13138 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13139 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 13140 | &connection_, kNewSelfAddress, connection_.peer_address())); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 13141 | EXPECT_EQ(1, connection_.GetStats().num_path_degrading); |
renjietang | 864fbda | 2022-09-08 10:50:07 -0700 | [diff] [blame] | 13142 | EXPECT_EQ(1, stats->num_multi_port_probe_failures_when_path_degrading); |
| 13143 | EXPECT_EQ(0, stats->num_multi_port_probe_failures_when_path_not_degrading); |
renjietang | fca5c77 | 2022-08-25 13:48:21 -0700 | [diff] [blame] | 13144 | } |
| 13145 | |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13146 | TEST_P(QuicConnectionTest, TooManyMultiPortPathCreations) { |
| 13147 | set_perspective(Perspective::IS_CLIENT); |
| 13148 | QuicConfig config; |
| 13149 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM}); |
| 13150 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
| 13151 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13152 | connection_.SetFromConfig(config); |
| 13153 | if (!connection_.connection_migration_use_new_cid()) { |
| 13154 | return; |
| 13155 | } |
| 13156 | connection_.CreateConnectionIdManager(); |
| 13157 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13158 | connection_.OnHandshakeComplete(); |
| 13159 | |
| 13160 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 13161 | connection_.OnPathDegradingDetected(); |
| 13162 | |
| 13163 | auto self_address = connection_.self_address(); |
| 13164 | const QuicSocketAddress kNewSelfAddress(self_address.host(), |
| 13165 | self_address.port() + 1); |
| 13166 | EXPECT_NE(kNewSelfAddress, self_address); |
| 13167 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 13168 | |
| 13169 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 13170 | .WillRepeatedly(Return(true)); |
| 13171 | |
| 13172 | QuicNewConnectionIdFrame frame; |
| 13173 | frame.connection_id = TestConnectionId(1234); |
| 13174 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13175 | frame.stateless_reset_token = |
| 13176 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13177 | frame.retire_prior_to = 0u; |
| 13178 | frame.sequence_number = 1u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13179 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13180 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13181 | observer->OnMultiPortPathContextAvailable( |
| 13182 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13183 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13184 | })); |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13185 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 13186 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13187 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13188 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13189 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13190 | EXPECT_FALSE(alt_path->validated); |
| 13191 | |
| 13192 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13193 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13194 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13195 | for (size_t i = 0; i < QuicPathValidator::kMaxRetryTimes + 1; ++i) { |
| 13196 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 13197 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 13198 | QuicPathValidatorPeer::retry_timer( |
| 13199 | QuicConnectionPeer::path_validator(&connection_))) |
| 13200 | ->Fire(); |
| 13201 | } |
| 13202 | |
| 13203 | auto stats = connection_.multi_port_stats(); |
| 13204 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13205 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 13206 | &connection_, kNewSelfAddress, connection_.peer_address())); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 13207 | EXPECT_EQ(1, connection_.GetStats().num_path_degrading); |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13208 | EXPECT_EQ(1, stats->num_multi_port_probe_failures_when_path_degrading); |
| 13209 | |
| 13210 | uint64_t connection_id = 1235; |
| 13211 | for (size_t i = 0; i < kMaxNumMultiPortPaths - 1; ++i) { |
| 13212 | QuicNewConnectionIdFrame frame; |
| 13213 | frame.connection_id = TestConnectionId(connection_id + i); |
| 13214 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13215 | frame.stateless_reset_token = |
| 13216 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13217 | frame.retire_prior_to = 0u; |
| 13218 | frame.sequence_number = i + 2; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13219 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13220 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13221 | observer->OnMultiPortPathContextAvailable( |
| 13222 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13223 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13224 | })); |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13225 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 13226 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13227 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13228 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13229 | EXPECT_FALSE(alt_path->validated); |
| 13230 | |
| 13231 | for (size_t j = 0; j < QuicPathValidator::kMaxRetryTimes + 1; ++j) { |
| 13232 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 13233 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 13234 | QuicPathValidatorPeer::retry_timer( |
| 13235 | QuicConnectionPeer::path_validator(&connection_))) |
| 13236 | ->Fire(); |
| 13237 | } |
| 13238 | |
| 13239 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13240 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 13241 | &connection_, kNewSelfAddress, connection_.peer_address())); |
renjietang | 201bfa5 | 2023-04-24 18:12:27 -0700 | [diff] [blame] | 13242 | EXPECT_EQ(1, connection_.GetStats().num_path_degrading); |
renjietang | 89540a6 | 2022-12-01 14:46:26 -0800 | [diff] [blame] | 13243 | EXPECT_EQ(i + 2, stats->num_multi_port_probe_failures_when_path_degrading); |
| 13244 | } |
| 13245 | |
| 13246 | // The 6th attemp should fail. |
| 13247 | QuicNewConnectionIdFrame frame2; |
| 13248 | frame2.connection_id = TestConnectionId(1239); |
| 13249 | ASSERT_NE(frame2.connection_id, connection_.connection_id()); |
| 13250 | frame2.stateless_reset_token = |
| 13251 | QuicUtils::GenerateStatelessResetToken(frame2.connection_id); |
| 13252 | frame2.retire_prior_to = 0u; |
| 13253 | frame2.sequence_number = 6u; |
| 13254 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame2)); |
| 13255 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13256 | EXPECT_EQ(kMaxNumMultiPortPaths, |
| 13257 | stats->num_multi_port_probe_failures_when_path_degrading); |
| 13258 | } |
| 13259 | |
renjietang | 5ba0e1b | 2023-03-14 13:08:03 -0700 | [diff] [blame] | 13260 | // Verify that when multi-port is enabled and path degrading is triggered, if |
| 13261 | // the alt-path is not ready, nothing happens. |
| 13262 | TEST_P(QuicConnectionTest, PathDegradingWhenAltPathIsNotReady) { |
| 13263 | set_perspective(Perspective::IS_CLIENT); |
| 13264 | QuicConfig config; |
| 13265 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM}); |
| 13266 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
| 13267 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13268 | connection_.SetFromConfig(config); |
| 13269 | if (!connection_.connection_migration_use_new_cid()) { |
| 13270 | return; |
| 13271 | } |
| 13272 | connection_.CreateConnectionIdManager(); |
| 13273 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13274 | connection_.OnHandshakeComplete(); |
| 13275 | |
| 13276 | auto self_address = connection_.self_address(); |
| 13277 | const QuicSocketAddress kNewSelfAddress(self_address.host(), |
| 13278 | self_address.port() + 1); |
| 13279 | EXPECT_NE(kNewSelfAddress, self_address); |
| 13280 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 13281 | |
| 13282 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 13283 | .WillRepeatedly(Return(true)); |
| 13284 | |
| 13285 | QuicNewConnectionIdFrame frame; |
| 13286 | frame.connection_id = TestConnectionId(1234); |
| 13287 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13288 | frame.stateless_reset_token = |
| 13289 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13290 | frame.retire_prior_to = 0u; |
| 13291 | frame.sequence_number = 1u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13292 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13293 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13294 | observer->OnMultiPortPathContextAvailable( |
| 13295 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13296 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13297 | })); |
renjietang | 5ba0e1b | 2023-03-14 13:08:03 -0700 | [diff] [blame] | 13298 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 13299 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13300 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13301 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13302 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13303 | EXPECT_FALSE(alt_path->validated); |
| 13304 | |
| 13305 | // The alt path is not ready, path degrading doesn't do anything. |
| 13306 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 13307 | EXPECT_CALL(visitor_, MigrateToMultiPortPath(_)).Times(0); |
| 13308 | connection_.OnPathDegradingDetected(); |
| 13309 | |
| 13310 | // 30ms RTT. |
| 13311 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 13312 | // Fake a response delay. |
| 13313 | clock_.AdvanceTime(kTestRTT); |
| 13314 | |
| 13315 | // Even if the alt path is validated after path degrading, nothing should |
| 13316 | // happen. |
| 13317 | QuicFrames frames; |
| 13318 | frames.push_back(QuicFrame(QuicPathResponseFrame( |
| 13319 | 99, new_writer.path_challenge_frames().back().data_buffer))); |
| 13320 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 13321 | ENCRYPTION_FORWARD_SECURE); |
| 13322 | // No migration should happen and the alternative path should still be alive. |
| 13323 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13324 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13325 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13326 | EXPECT_TRUE(alt_path->validated); |
| 13327 | } |
| 13328 | |
| 13329 | // Verify that when multi-port is enabled and path degrading is triggered, if |
| 13330 | // the alt-path is ready and not probing, it should be migrated. |
| 13331 | TEST_P(QuicConnectionTest, PathDegradingWhenAltPathIsReadyAndNotProbing) { |
| 13332 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13333 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 13334 | set_perspective(Perspective::IS_CLIENT); |
| 13335 | QuicConfig config; |
| 13336 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM}); |
| 13337 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
| 13338 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13339 | connection_.SetFromConfig(config); |
| 13340 | if (!connection_.connection_migration_use_new_cid()) { |
| 13341 | return; |
| 13342 | } |
| 13343 | connection_.CreateConnectionIdManager(); |
| 13344 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13345 | connection_.OnHandshakeComplete(); |
| 13346 | |
| 13347 | auto self_address = connection_.self_address(); |
| 13348 | const QuicSocketAddress kNewSelfAddress(self_address.host(), |
| 13349 | self_address.port() + 1); |
| 13350 | EXPECT_NE(kNewSelfAddress, self_address); |
| 13351 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 13352 | |
| 13353 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 13354 | .WillRepeatedly(Return(true)); |
| 13355 | |
| 13356 | QuicNewConnectionIdFrame frame; |
| 13357 | frame.connection_id = TestConnectionId(1234); |
| 13358 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13359 | frame.stateless_reset_token = |
| 13360 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13361 | frame.retire_prior_to = 0u; |
| 13362 | frame.sequence_number = 1u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13363 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13364 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13365 | observer->OnMultiPortPathContextAvailable( |
| 13366 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13367 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13368 | })); |
renjietang | 5ba0e1b | 2023-03-14 13:08:03 -0700 | [diff] [blame] | 13369 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 13370 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13371 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13372 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13373 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13374 | EXPECT_FALSE(alt_path->validated); |
| 13375 | |
| 13376 | // 30ms RTT. |
| 13377 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 13378 | // Fake a response delay. |
| 13379 | clock_.AdvanceTime(kTestRTT); |
| 13380 | |
| 13381 | // Even if the alt path is validated after path degrading, nothing should |
| 13382 | // happen. |
| 13383 | QuicFrames frames; |
| 13384 | frames.push_back(QuicFrame(QuicPathResponseFrame( |
| 13385 | 99, new_writer.path_challenge_frames().back().data_buffer))); |
| 13386 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 13387 | ENCRYPTION_FORWARD_SECURE); |
| 13388 | // No migration should happen and the alternative path should still be alive. |
| 13389 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13390 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13391 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13392 | EXPECT_TRUE(alt_path->validated); |
| 13393 | |
| 13394 | // Trigger path degrading and the connection should attempt to migrate. |
| 13395 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 13396 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()).Times(0); |
| 13397 | EXPECT_CALL(visitor_, MigrateToMultiPortPath(_)) |
| 13398 | .WillOnce(Invoke([&](std::unique_ptr<QuicPathValidationContext> context) { |
| 13399 | EXPECT_EQ(context->self_address(), kNewSelfAddress); |
| 13400 | connection_.MigratePath(context->self_address(), |
| 13401 | context->peer_address(), context->WriterToUse(), |
| 13402 | /*owns_writer=*/false); |
| 13403 | })); |
| 13404 | connection_.OnPathDegradingDetected(); |
| 13405 | } |
| 13406 | |
| 13407 | // Verify that when multi-port is enabled and path degrading is triggered, if |
| 13408 | // the alt-path is probing, the probing should be cancelled and the path should |
| 13409 | // be migrated. |
| 13410 | TEST_P(QuicConnectionTest, PathDegradingWhenAltPathIsReadyAndProbing) { |
| 13411 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13412 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 13413 | set_perspective(Perspective::IS_CLIENT); |
| 13414 | QuicConfig config; |
| 13415 | config.SetConnectionOptionsToSend(QuicTagVector{kRVCM}); |
| 13416 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
| 13417 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13418 | connection_.SetFromConfig(config); |
| 13419 | if (!connection_.connection_migration_use_new_cid()) { |
| 13420 | return; |
| 13421 | } |
| 13422 | connection_.CreateConnectionIdManager(); |
| 13423 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13424 | connection_.OnHandshakeComplete(); |
| 13425 | |
| 13426 | auto self_address = connection_.self_address(); |
| 13427 | const QuicSocketAddress kNewSelfAddress(self_address.host(), |
| 13428 | self_address.port() + 1); |
| 13429 | EXPECT_NE(kNewSelfAddress, self_address); |
| 13430 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 13431 | |
| 13432 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 13433 | .WillRepeatedly(Return(true)); |
| 13434 | |
| 13435 | QuicNewConnectionIdFrame frame; |
| 13436 | frame.connection_id = TestConnectionId(1234); |
| 13437 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 13438 | frame.stateless_reset_token = |
| 13439 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 13440 | frame.retire_prior_to = 0u; |
| 13441 | frame.sequence_number = 1u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13442 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 13443 | .WillRepeatedly(testing::WithArgs<0>([&](auto&& observer) { |
| 13444 | observer->OnMultiPortPathContextAvailable( |
| 13445 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 13446 | kNewSelfAddress, connection_.peer_address(), &new_writer))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 13447 | })); |
renjietang | 5ba0e1b | 2023-03-14 13:08:03 -0700 | [diff] [blame] | 13448 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 13449 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13450 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13451 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13452 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13453 | EXPECT_FALSE(alt_path->validated); |
| 13454 | |
| 13455 | // 30ms RTT. |
| 13456 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 13457 | // Fake a response delay. |
| 13458 | clock_.AdvanceTime(kTestRTT); |
| 13459 | |
| 13460 | // Even if the alt path is validated after path degrading, nothing should |
| 13461 | // happen. |
| 13462 | QuicFrames frames; |
| 13463 | frames.push_back(QuicFrame(QuicPathResponseFrame( |
| 13464 | 99, new_writer.path_challenge_frames().back().data_buffer))); |
| 13465 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 13466 | ENCRYPTION_FORWARD_SECURE); |
| 13467 | // No migration should happen and the alternative path should still be alive. |
| 13468 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13469 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 13470 | &connection_, kNewSelfAddress, connection_.peer_address())); |
| 13471 | EXPECT_TRUE(alt_path->validated); |
| 13472 | |
| 13473 | random_generator_.ChangeValue(); |
| 13474 | connection_.GetMultiPortProbingAlarm()->Fire(); |
| 13475 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13476 | EXPECT_FALSE(connection_.GetMultiPortProbingAlarm()->IsSet()); |
| 13477 | |
| 13478 | // Trigger path degrading and the connection should attempt to migrate. |
| 13479 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 13480 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()).Times(0); |
| 13481 | EXPECT_CALL(visitor_, MigrateToMultiPortPath(_)) |
| 13482 | .WillOnce(Invoke([&](std::unique_ptr<QuicPathValidationContext> context) { |
| 13483 | EXPECT_EQ(context->self_address(), kNewSelfAddress); |
| 13484 | connection_.MigratePath(context->self_address(), |
| 13485 | context->peer_address(), context->WriterToUse(), |
| 13486 | /*owns_writer=*/false); |
| 13487 | })); |
| 13488 | connection_.OnPathDegradingDetected(); |
| 13489 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 13490 | auto* path_validator = QuicConnectionPeer::path_validator(&connection_); |
| 13491 | EXPECT_FALSE(QuicPathValidatorPeer::retry_timer(path_validator)->IsSet()); |
| 13492 | } |
| 13493 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13494 | TEST_P(QuicConnectionTest, SingleAckInPacket) { |
| 13495 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 13496 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 13497 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13498 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 13499 | connection_.NeuterUnencryptedPackets(); |
| 13500 | connection_.OnHandshakeComplete(); |
| 13501 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13502 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 13503 | |
| 13504 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([=]() { |
| 13505 | connection_.SendStreamData3(); |
| 13506 | connection_.CloseConnection( |
| 13507 | QUIC_INTERNAL_ERROR, "error", |
| 13508 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 13509 | })); |
| 13510 | QuicFrames frames; |
| 13511 | frames.push_back(QuicFrame(frame1_)); |
| 13512 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 13513 | ENCRYPTION_FORWARD_SECURE); |
| 13514 | ASSERT_FALSE(writer_->ack_frames().empty()); |
| 13515 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 13516 | } |
| 13517 | |
| 13518 | TEST_P(QuicConnectionTest, |
| 13519 | ServerReceivedZeroRttPacketAfterOneRttPacketWithRetainedKey) { |
| 13520 | if (!connection_.version().UsesTls()) { |
| 13521 | return; |
| 13522 | } |
| 13523 | |
| 13524 | set_perspective(Perspective::IS_SERVER); |
| 13525 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 13526 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13527 | |
| 13528 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13529 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 13530 | |
| 13531 | // Finish handshake. |
| 13532 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13533 | notifier_.NeuterUnencryptedData(); |
| 13534 | connection_.NeuterUnencryptedPackets(); |
| 13535 | connection_.OnHandshakeComplete(); |
| 13536 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13537 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 13538 | |
| 13539 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13540 | ProcessDataPacketAtLevel(4, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 13541 | EXPECT_TRUE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 13542 | |
| 13543 | // 0-RTT packet received out of order should be decoded since the decrypter |
| 13544 | // is temporarily retained. |
| 13545 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13546 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 13547 | EXPECT_EQ( |
| 13548 | 0u, |
| 13549 | connection_.GetStats() |
| 13550 | .num_tls_server_zero_rtt_packets_received_after_discarding_decrypter); |
| 13551 | |
| 13552 | // Simulate the timeout for discarding 0-RTT keys passing. |
| 13553 | connection_.GetDiscardZeroRttDecryptionKeysAlarm()->Fire(); |
| 13554 | |
| 13555 | // Another 0-RTT packet received now should not be decoded. |
| 13556 | EXPECT_FALSE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 13557 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(0); |
| 13558 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 13559 | EXPECT_EQ( |
| 13560 | 1u, |
| 13561 | connection_.GetStats() |
| 13562 | .num_tls_server_zero_rtt_packets_received_after_discarding_decrypter); |
| 13563 | |
| 13564 | // The |discard_zero_rtt_decryption_keys_alarm_| should only be set on the |
| 13565 | // first 1-RTT packet received. |
| 13566 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13567 | ProcessDataPacketAtLevel(5, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 13568 | EXPECT_FALSE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 13569 | } |
| 13570 | |
| 13571 | TEST_P(QuicConnectionTest, NewTokenFrameInstigateAcks) { |
| 13572 | if (!version().HasIetfQuicFrames()) { |
| 13573 | return; |
| 13574 | } |
| 13575 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 13576 | |
| 13577 | QuicNewTokenFrame* new_token = new QuicNewTokenFrame(); |
| 13578 | EXPECT_CALL(visitor_, OnNewTokenReceived(_)); |
| 13579 | ProcessFramePacket(QuicFrame(new_token)); |
| 13580 | |
| 13581 | // Ensure that this has caused the ACK alarm to be set. |
| 13582 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 13583 | } |
| 13584 | |
| 13585 | TEST_P(QuicConnectionTest, ServerClosesConnectionOnNewTokenFrame) { |
| 13586 | if (!version().HasIetfQuicFrames()) { |
| 13587 | return; |
| 13588 | } |
| 13589 | set_perspective(Perspective::IS_SERVER); |
| 13590 | QuicNewTokenFrame* new_token = new QuicNewTokenFrame(); |
| 13591 | EXPECT_CALL(visitor_, OnNewTokenReceived(_)).Times(0); |
| 13592 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 13593 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 13594 | ProcessFramePacket(QuicFrame(new_token)); |
| 13595 | EXPECT_FALSE(connection_.connected()); |
| 13596 | } |
| 13597 | |
| 13598 | TEST_P(QuicConnectionTest, OverrideRetryTokenWithRetryPacket) { |
| 13599 | if (!version().HasIetfQuicFrames()) { |
| 13600 | return; |
| 13601 | } |
| 13602 | std::string address_token = "TestAddressToken"; |
| 13603 | connection_.SetSourceAddressTokenToSend(address_token); |
| 13604 | EXPECT_EQ(QuicPacketCreatorPeer::GetRetryToken( |
| 13605 | QuicConnectionPeer::GetPacketCreator(&connection_)), |
| 13606 | address_token); |
| 13607 | // Passes valid retry and verify token gets overridden. |
| 13608 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 13609 | /*missing_original_id_in_config=*/false, |
| 13610 | /*wrong_original_id_in_config=*/false, |
| 13611 | /*missing_retry_id_in_config=*/false, |
| 13612 | /*wrong_retry_id_in_config=*/false); |
| 13613 | } |
| 13614 | |
| 13615 | TEST_P(QuicConnectionTest, DonotOverrideRetryTokenWithAddressToken) { |
| 13616 | if (!version().HasIetfQuicFrames()) { |
| 13617 | return; |
| 13618 | } |
| 13619 | // Passes valid retry and verify token gets overridden. |
| 13620 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 13621 | /*missing_original_id_in_config=*/false, |
| 13622 | /*wrong_original_id_in_config=*/false, |
| 13623 | /*missing_retry_id_in_config=*/false, |
| 13624 | /*wrong_retry_id_in_config=*/false); |
| 13625 | std::string retry_token = QuicPacketCreatorPeer::GetRetryToken( |
| 13626 | QuicConnectionPeer::GetPacketCreator(&connection_)); |
| 13627 | |
| 13628 | std::string address_token = "TestAddressToken"; |
| 13629 | connection_.SetSourceAddressTokenToSend(address_token); |
| 13630 | EXPECT_EQ(QuicPacketCreatorPeer::GetRetryToken( |
| 13631 | QuicConnectionPeer::GetPacketCreator(&connection_)), |
| 13632 | retry_token); |
| 13633 | } |
| 13634 | |
| 13635 | TEST_P(QuicConnectionTest, |
| 13636 | ServerReceivedZeroRttWithHigherPacketNumberThanOneRtt) { |
| 13637 | if (!connection_.version().UsesTls()) { |
| 13638 | return; |
| 13639 | } |
| 13640 | |
| 13641 | // The code that checks for this error piggybacks on some book-keeping state |
| 13642 | // kept for key update, so enable key update for the test. |
| 13643 | std::string error_details; |
| 13644 | TransportParameters params; |
| 13645 | QuicConfig config; |
| 13646 | EXPECT_THAT(config.ProcessTransportParameters( |
| 13647 | params, /* is_resumption = */ false, &error_details), |
| 13648 | IsQuicNoError()); |
| 13649 | QuicConfigPeer::SetNegotiated(&config, true); |
| 13650 | QuicConfigPeer::SetReceivedOriginalConnectionId(&config, |
| 13651 | connection_.connection_id()); |
| 13652 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 13653 | &config, connection_.connection_id()); |
| 13654 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13655 | connection_.SetFromConfig(config); |
| 13656 | |
| 13657 | set_perspective(Perspective::IS_SERVER); |
| 13658 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 13659 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13660 | |
| 13661 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13662 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 13663 | |
| 13664 | // Finish handshake. |
| 13665 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13666 | notifier_.NeuterUnencryptedData(); |
| 13667 | connection_.NeuterUnencryptedPackets(); |
| 13668 | connection_.OnHandshakeComplete(); |
| 13669 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13670 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 13671 | |
| 13672 | // Decrypt a 1-RTT packet. |
| 13673 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13674 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 13675 | EXPECT_TRUE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 13676 | |
| 13677 | // 0-RTT packet with higher packet number than a 1-RTT packet is invalid and |
| 13678 | // should cause the connection to be closed. |
| 13679 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 13680 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 13681 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 13682 | EXPECT_FALSE(connection_.connected()); |
| 13683 | TestConnectionCloseQuicErrorCode( |
| 13684 | QUIC_INVALID_0RTT_PACKET_NUMBER_OUT_OF_ORDER); |
| 13685 | } |
| 13686 | |
| 13687 | // Regression test for b/177312785 |
| 13688 | TEST_P(QuicConnectionTest, PeerMigrateBeforeHandshakeConfirm) { |
| 13689 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 13690 | return; |
| 13691 | } |
| 13692 | set_perspective(Perspective::IS_SERVER); |
| 13693 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 13694 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 13695 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 13696 | .WillRepeatedly(Return(HANDSHAKE_START)); |
| 13697 | |
| 13698 | // Clear direct_peer_address. |
| 13699 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 13700 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 13701 | // this test. |
| 13702 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 13703 | QuicSocketAddress()); |
| 13704 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 13705 | |
| 13706 | const QuicSocketAddress kNewPeerAddress = |
| 13707 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 13708 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 13709 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, kPeerAddress, |
| 13710 | ENCRYPTION_INITIAL); |
| 13711 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 13712 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 13713 | |
| 13714 | // Process another packet with a different peer address on server side will |
| 13715 | // close connection. |
| 13716 | QuicAckFrame frame = InitAckFrame(1); |
| 13717 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 13718 | EXPECT_CALL(visitor_, |
| 13719 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
| 13720 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0u); |
| 13721 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 13722 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
| 13723 | .Times(0); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13724 | ProcessFramePacketWithAddresses(QuicFrame(&frame), kSelfAddress, |
| 13725 | kNewPeerAddress, ENCRYPTION_INITIAL); |
| 13726 | EXPECT_FALSE(connection_.connected()); |
| 13727 | } |
| 13728 | |
| 13729 | // Regresstion test for b/175685916 |
| 13730 | TEST_P(QuicConnectionTest, TryToFlushAckWithAckQueued) { |
| 13731 | if (!version().HasIetfQuicFrames()) { |
| 13732 | return; |
| 13733 | } |
| 13734 | SetQuicReloadableFlag(quic_can_send_ack_frequency, true); |
| 13735 | set_perspective(Perspective::IS_SERVER); |
| 13736 | |
| 13737 | QuicConfig config; |
| 13738 | QuicConfigPeer::SetReceivedMinAckDelayMs(&config, /*min_ack_delay_ms=*/1); |
| 13739 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 13740 | connection_.SetFromConfig(config); |
| 13741 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 13742 | connection_.OnHandshakeComplete(); |
| 13743 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 200); |
| 13744 | |
| 13745 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 13746 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 13747 | // Sending ACK_FREQUENCY bundles ACK. QuicConnectionPeer::SendPing |
| 13748 | // will try to bundle ACK but there is no pending ACK. |
| 13749 | EXPECT_CALL(visitor_, SendAckFrequency(_)) |
| 13750 | .WillOnce(Invoke(¬ifier_, |
| 13751 | &SimpleSessionNotifier::WriteOrBufferAckFrequency)); |
| 13752 | QuicConnectionPeer::SendPing(&connection_); |
| 13753 | } |
| 13754 | |
| 13755 | TEST_P(QuicConnectionTest, PathChallengeBeforePeerIpAddressChangeAtServer) { |
| 13756 | set_perspective(Perspective::IS_SERVER); |
| 13757 | if (!connection_.connection_migration_use_new_cid()) { |
| 13758 | return; |
| 13759 | } |
| 13760 | PathProbeTestInit(Perspective::IS_SERVER); |
| 13761 | SetClientConnectionId(TestConnectionId(1)); |
| 13762 | connection_.CreateConnectionIdManager(); |
| 13763 | |
| 13764 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 13765 | QuicConnectionId client_cid0 = connection_.client_connection_id(); |
| 13766 | QuicConnectionId client_cid1 = TestConnectionId(2); |
| 13767 | QuicConnectionId server_cid1; |
| 13768 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 13769 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 13770 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 13771 | .WillOnce(Return(TestConnectionId(456))); |
| 13772 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 13773 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 13774 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 13775 | server_cid1 = cid; |
| 13776 | return true; |
| 13777 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13778 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 13779 | connection_.MaybeSendConnectionIdToClient(); |
| 13780 | // Receives new client CID from client. |
| 13781 | QuicNewConnectionIdFrame new_cid_frame; |
| 13782 | new_cid_frame.connection_id = client_cid1; |
| 13783 | new_cid_frame.sequence_number = 1u; |
| 13784 | new_cid_frame.retire_prior_to = 0u; |
| 13785 | connection_.OnNewConnectionIdFrame(new_cid_frame); |
| 13786 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 13787 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 13788 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 13789 | |
| 13790 | peer_creator_.SetServerConnectionId(server_cid1); |
| 13791 | const QuicSocketAddress kNewPeerAddress = |
| 13792 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 13793 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 13794 | QuicFrames frames1; |
| 13795 | frames1.push_back( |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 13796 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13797 | QuicPathFrameBuffer payload; |
| 13798 | EXPECT_CALL(*send_algorithm_, |
| 13799 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 13800 | .Times(AtLeast(1)) |
| 13801 | .WillOnce(Invoke([&]() { |
| 13802 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 13803 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 13804 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 13805 | EXPECT_FALSE(writer_->path_response_frames().empty()); |
| 13806 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 13807 | payload = writer_->path_challenge_frames().front().data_buffer; |
| 13808 | })); |
| 13809 | ProcessFramesPacketWithAddresses(frames1, kSelfAddress, kNewPeerAddress, |
| 13810 | ENCRYPTION_FORWARD_SECURE); |
| 13811 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 13812 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 13813 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13814 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 13815 | const auto* alternative_path = |
| 13816 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13817 | EXPECT_EQ(default_path->client_connection_id, client_cid0); |
| 13818 | EXPECT_EQ(default_path->server_connection_id, server_cid0); |
| 13819 | EXPECT_EQ(alternative_path->client_connection_id, client_cid1); |
| 13820 | EXPECT_EQ(alternative_path->server_connection_id, server_cid1); |
| 13821 | EXPECT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 13822 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 13823 | |
| 13824 | // Process another packet with a different peer address on server side will |
| 13825 | // start connection migration. |
| 13826 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 13827 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([=]() { |
| 13828 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 13829 | })); |
| 13830 | // IETF QUIC send algorithm should be changed to a different object, so no |
| 13831 | // OnPacketSent() called on the old send algorithm. |
| 13832 | EXPECT_CALL(*send_algorithm_, |
| 13833 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 13834 | .Times(0); |
| 13835 | QuicFrames frames2; |
| 13836 | frames2.push_back(QuicFrame(frame2_)); |
| 13837 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 13838 | ENCRYPTION_FORWARD_SECURE); |
| 13839 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 13840 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 13841 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 13842 | connection_.active_effective_peer_migration_type()); |
| 13843 | EXPECT_TRUE(writer_->path_challenge_frames().empty()); |
| 13844 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 13845 | send_algorithm_); |
| 13846 | // Switch to use the mock send algorithm. |
| 13847 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 13848 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 13849 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 13850 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 13851 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 13852 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 13853 | .Times(AnyNumber()) |
| 13854 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 13855 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 13856 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 13857 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 13858 | connection_.SetSendAlgorithm(send_algorithm_); |
| 13859 | EXPECT_EQ(default_path->client_connection_id, client_cid1); |
| 13860 | EXPECT_EQ(default_path->server_connection_id, server_cid1); |
| 13861 | // The previous default path is kept as alternative path before reverse path |
| 13862 | // validation finishes. |
| 13863 | EXPECT_EQ(alternative_path->client_connection_id, client_cid0); |
| 13864 | EXPECT_EQ(alternative_path->server_connection_id, server_cid0); |
| 13865 | EXPECT_EQ(packet_creator->GetDestinationConnectionId(), client_cid1); |
| 13866 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid1); |
| 13867 | |
| 13868 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 13869 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 13870 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 13871 | connection_.active_effective_peer_migration_type()); |
| 13872 | EXPECT_EQ(1u, connection_.GetStats() |
| 13873 | .num_peer_migration_to_proactively_validated_address); |
| 13874 | |
| 13875 | // The PATH_CHALLENGE and PATH_RESPONSE is expanded upto the max packet size |
| 13876 | // which may exceeds the anti-amplification limit. Verify server is throttled |
| 13877 | // by anti-amplification limit. |
| 13878 | connection_.SendCryptoDataWithString("foo", 0); |
| 13879 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 13880 | |
| 13881 | // Receiving PATH_RESPONSE should lift the anti-amplification limit. |
| 13882 | QuicFrames frames3; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 13883 | frames3.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13884 | EXPECT_CALL(visitor_, MaybeSendAddressToken()); |
| 13885 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 13886 | .Times(testing::AtLeast(1u)); |
| 13887 | ProcessFramesPacketWithAddresses(frames3, kSelfAddress, kNewPeerAddress, |
| 13888 | ENCRYPTION_FORWARD_SECURE); |
| 13889 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 13890 | // Verify that alternative_path_ is cleared and the peer CID is retired. |
| 13891 | EXPECT_TRUE(alternative_path->client_connection_id.IsEmpty()); |
| 13892 | EXPECT_TRUE(alternative_path->server_connection_id.IsEmpty()); |
| 13893 | EXPECT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 13894 | auto* retire_peer_issued_cid_alarm = |
| 13895 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 13896 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 13897 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 13898 | retire_peer_issued_cid_alarm->Fire(); |
| 13899 | |
| 13900 | // Verify the anti-amplification limit is lifted by sending a packet larger |
| 13901 | // than the anti-amplification limit. |
| 13902 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 13903 | EXPECT_CALL(*send_algorithm_, PacingRate(_)) |
| 13904 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 13905 | connection_.SendCryptoDataWithString(std::string(1200, 'a'), 0); |
| 13906 | EXPECT_EQ(1u, connection_.GetStats().num_validated_peer_migration); |
renjietang | ff3e960 | 2022-10-25 12:16:49 -0700 | [diff] [blame] | 13907 | EXPECT_EQ(1u, connection_.num_unlinkable_client_migration()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13908 | } |
| 13909 | |
| 13910 | TEST_P(QuicConnectionTest, |
| 13911 | PathValidationSucceedsBeforePeerIpAddressChangeAtServer) { |
| 13912 | set_perspective(Perspective::IS_SERVER); |
| 13913 | if (!connection_.connection_migration_use_new_cid()) { |
| 13914 | return; |
| 13915 | } |
| 13916 | PathProbeTestInit(Perspective::IS_SERVER); |
| 13917 | connection_.CreateConnectionIdManager(); |
| 13918 | |
| 13919 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 13920 | QuicConnectionId server_cid1; |
| 13921 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 13922 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 13923 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 13924 | .WillOnce(Return(TestConnectionId(456))); |
| 13925 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 13926 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 13927 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 13928 | server_cid1 = cid; |
| 13929 | return true; |
| 13930 | })); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13931 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 13932 | connection_.MaybeSendConnectionIdToClient(); |
| 13933 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 13934 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 13935 | |
| 13936 | // Receive probing packet with new peer address. |
| 13937 | peer_creator_.SetServerConnectionId(server_cid1); |
| 13938 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 13939 | /*port=*/23456); |
| 13940 | QuicPathFrameBuffer payload; |
| 13941 | EXPECT_CALL(*send_algorithm_, |
| 13942 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 13943 | .WillOnce(Invoke([&]() { |
| 13944 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 13945 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 13946 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 13947 | EXPECT_FALSE(writer_->path_response_frames().empty()); |
| 13948 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 13949 | payload = writer_->path_challenge_frames().front().data_buffer; |
| 13950 | })) |
| 13951 | .WillRepeatedly(Invoke([&]() { |
| 13952 | // Only start reverse path validation once. |
| 13953 | EXPECT_TRUE(writer_->path_challenge_frames().empty()); |
| 13954 | })); |
| 13955 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 13956 | QuicFrames frames1; |
| 13957 | frames1.push_back( |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 13958 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13959 | ProcessFramesPacketWithAddresses(frames1, kSelfAddress, kNewPeerAddress, |
| 13960 | ENCRYPTION_FORWARD_SECURE); |
| 13961 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 13962 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 13963 | const auto* alternative_path = |
| 13964 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 13965 | EXPECT_EQ(default_path->server_connection_id, server_cid0); |
| 13966 | EXPECT_EQ(alternative_path->server_connection_id, server_cid1); |
| 13967 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 13968 | |
| 13969 | // Receive PATH_RESPONSE should mark the new peer address validated. |
| 13970 | QuicFrames frames3; |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 13971 | frames3.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13972 | ProcessFramesPacketWithAddresses(frames3, kSelfAddress, kNewPeerAddress, |
| 13973 | ENCRYPTION_FORWARD_SECURE); |
| 13974 | |
| 13975 | // Process another packet with a newer peer address with the same port will |
| 13976 | // start connection migration. |
| 13977 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 13978 | // IETF QUIC send algorithm should be changed to a different object, so no |
| 13979 | // OnPacketSent() called on the old send algorithm. |
| 13980 | EXPECT_CALL(*send_algorithm_, |
| 13981 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 13982 | .Times(0); |
| 13983 | const QuicSocketAddress kNewerPeerAddress(QuicIpAddress::Loopback4(), |
| 13984 | /*port=*/34567); |
| 13985 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([=]() { |
| 13986 | EXPECT_EQ(kNewerPeerAddress, connection_.peer_address()); |
| 13987 | })); |
| 13988 | EXPECT_CALL(visitor_, MaybeSendAddressToken()); |
| 13989 | QuicFrames frames2; |
| 13990 | frames2.push_back(QuicFrame(frame2_)); |
| 13991 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewerPeerAddress, |
| 13992 | ENCRYPTION_FORWARD_SECURE); |
| 13993 | EXPECT_EQ(kNewerPeerAddress, connection_.peer_address()); |
| 13994 | EXPECT_EQ(kNewerPeerAddress, connection_.effective_peer_address()); |
| 13995 | // Since the newer address has the same IP as the previously validated probing |
| 13996 | // address. The peer migration becomes validated immediately. |
| 13997 | EXPECT_EQ(NO_CHANGE, connection_.active_effective_peer_migration_type()); |
| 13998 | EXPECT_EQ(kNewerPeerAddress, writer_->last_write_peer_address()); |
| 13999 | EXPECT_EQ(1u, connection_.GetStats() |
| 14000 | .num_peer_migration_to_proactively_validated_address); |
| 14001 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 14002 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 14003 | send_algorithm_); |
| 14004 | |
| 14005 | EXPECT_EQ(default_path->server_connection_id, server_cid1); |
| 14006 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid1); |
| 14007 | // Verify that alternative_path_ is cleared. |
| 14008 | EXPECT_TRUE(alternative_path->server_connection_id.IsEmpty()); |
| 14009 | EXPECT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 14010 | |
| 14011 | // Switch to use the mock send algorithm. |
| 14012 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 14013 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 14014 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 14015 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 14016 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 14017 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 14018 | .Times(AnyNumber()) |
| 14019 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 14020 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 14021 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 14022 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 14023 | connection_.SetSendAlgorithm(send_algorithm_); |
| 14024 | |
| 14025 | // Verify the server is not throttled by the anti-amplification limit by |
| 14026 | // sending a packet larger than the anti-amplification limit. |
| 14027 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 14028 | connection_.SendCryptoDataWithString(std::string(1200, 'a'), 0); |
| 14029 | EXPECT_EQ(1u, connection_.GetStats().num_validated_peer_migration); |
| 14030 | } |
| 14031 | |
danzh | b37451f | 2022-04-19 08:18:54 -0700 | [diff] [blame] | 14032 | // Regression test of b/228645208. |
| 14033 | TEST_P(QuicConnectionTest, NoNonProbingFrameOnAlternativePath) { |
| 14034 | if (!connection_.connection_migration_use_new_cid()) { |
| 14035 | return; |
| 14036 | } |
| 14037 | |
| 14038 | PathProbeTestInit(Perspective::IS_SERVER); |
| 14039 | SetClientConnectionId(TestConnectionId(1)); |
| 14040 | connection_.CreateConnectionIdManager(); |
| 14041 | |
| 14042 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 14043 | QuicConnectionId client_cid0 = connection_.client_connection_id(); |
| 14044 | QuicConnectionId client_cid1 = TestConnectionId(2); |
| 14045 | QuicConnectionId server_cid1; |
| 14046 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14047 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14048 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14049 | .WillOnce(Return(TestConnectionId(456))); |
| 14050 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14051 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 14052 | .WillOnce(Invoke([&](const QuicConnectionId& cid) { |
| 14053 | server_cid1 = cid; |
| 14054 | return true; |
| 14055 | })); |
danzh | b37451f | 2022-04-19 08:18:54 -0700 | [diff] [blame] | 14056 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 14057 | connection_.MaybeSendConnectionIdToClient(); |
| 14058 | // Receives new client CID from client. |
| 14059 | QuicNewConnectionIdFrame new_cid_frame; |
| 14060 | new_cid_frame.connection_id = client_cid1; |
| 14061 | new_cid_frame.sequence_number = 1u; |
| 14062 | new_cid_frame.retire_prior_to = 0u; |
| 14063 | connection_.OnNewConnectionIdFrame(new_cid_frame); |
| 14064 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 14065 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 14066 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 14067 | |
| 14068 | peer_creator_.SetServerConnectionId(server_cid1); |
| 14069 | const QuicSocketAddress kNewPeerAddress = |
| 14070 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/23456); |
| 14071 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 14072 | QuicFrames frames1; |
| 14073 | frames1.push_back( |
| 14074 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
| 14075 | QuicPathFrameBuffer payload; |
| 14076 | EXPECT_CALL(*send_algorithm_, |
| 14077 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 14078 | .Times(AtLeast(1)) |
| 14079 | .WillOnce(Invoke([&]() { |
| 14080 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 14081 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 14082 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 14083 | EXPECT_FALSE(writer_->path_response_frames().empty()); |
| 14084 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 14085 | payload = writer_->path_challenge_frames().front().data_buffer; |
| 14086 | })); |
| 14087 | ProcessFramesPacketWithAddresses(frames1, kSelfAddress, kNewPeerAddress, |
| 14088 | ENCRYPTION_FORWARD_SECURE); |
| 14089 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 14090 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 14091 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 14092 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 14093 | const auto* alternative_path = |
| 14094 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 14095 | EXPECT_EQ(default_path->client_connection_id, client_cid0); |
| 14096 | EXPECT_EQ(default_path->server_connection_id, server_cid0); |
| 14097 | EXPECT_EQ(alternative_path->client_connection_id, client_cid1); |
| 14098 | EXPECT_EQ(alternative_path->server_connection_id, server_cid1); |
| 14099 | EXPECT_EQ(packet_creator->GetDestinationConnectionId(), client_cid0); |
| 14100 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), server_cid0); |
| 14101 | |
| 14102 | // Process non-probing packets on the default path. |
| 14103 | peer_creator_.SetServerConnectionId(server_cid0); |
| 14104 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillRepeatedly(Invoke([=]() { |
| 14105 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 14106 | })); |
| 14107 | // Receives packets 3 - 39 to send 19 ACK-only packets, which will force the |
| 14108 | // connection to reach |kMaxConsecutiveNonRetransmittablePackets| while |
| 14109 | // sending the next ACK. |
| 14110 | for (size_t i = 3; i <= 39; ++i) { |
| 14111 | ProcessDataPacket(i); |
| 14112 | } |
| 14113 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 14114 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 14115 | |
| 14116 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 14117 | QuicTime ack_time = connection_.GetAckAlarm()->deadline(); |
| 14118 | QuicTime path_validation_retry_time = |
| 14119 | connection_.GetRetryTimeout(kNewPeerAddress, writer_.get()); |
| 14120 | // Advance time to simultaneously fire path validation retry and ACK alarms. |
| 14121 | clock_.AdvanceTime(std::max(ack_time, path_validation_retry_time) - |
| 14122 | clock_.ApproximateNow()); |
| 14123 | |
| 14124 | // The 20th ACK should bundle with a WINDOW_UPDATE frame. |
| 14125 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 14126 | .WillOnce(Invoke([this]() { |
| 14127 | connection_.SendControlFrame(QuicFrame(QuicWindowUpdateFrame(1, 0, 0))); |
| 14128 | })); |
martinduke | e6444ef | 2022-09-23 12:32:23 -0700 | [diff] [blame] | 14129 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 14130 | .WillOnce(Invoke([&]() { |
| 14131 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 14132 | EXPECT_FALSE(writer_->path_challenge_frames().empty()); |
| 14133 | // Retry path validation shouldn't bundle ACK. |
| 14134 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 14135 | })) |
| 14136 | .WillOnce(Invoke([&]() { |
| 14137 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 14138 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 14139 | EXPECT_FALSE(writer_->window_update_frames().empty()); |
| 14140 | })); |
| 14141 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 14142 | QuicPathValidatorPeer::retry_timer( |
| 14143 | QuicConnectionPeer::path_validator(&connection_))) |
| 14144 | ->Fire(); |
danzh | b37451f | 2022-04-19 08:18:54 -0700 | [diff] [blame] | 14145 | } |
| 14146 | |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14147 | TEST_P(QuicConnectionTest, DoNotIssueNewCidIfVisitorSaysNo) { |
| 14148 | set_perspective(Perspective::IS_SERVER); |
| 14149 | if (!connection_.connection_migration_use_new_cid()) { |
| 14150 | return; |
| 14151 | } |
| 14152 | |
| 14153 | connection_.CreateConnectionIdManager(); |
| 14154 | |
| 14155 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 14156 | QuicConnectionId client_cid1 = TestConnectionId(2); |
| 14157 | QuicConnectionId server_cid1; |
| 14158 | // Sends new server CID to client. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14159 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14160 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14161 | .WillOnce(Return(TestConnectionId(456))); |
| 14162 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14163 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)).WillOnce(Return(false)); |
| 14164 | if (GetQuicReloadableFlag(quic_check_cid_collision_when_issue_new_cid)) { |
| 14165 | EXPECT_CALL(visitor_, SendNewConnectionId(_)).Times(0); |
| 14166 | } else { |
| 14167 | EXPECT_CALL(visitor_, SendNewConnectionId(_)).Times(1); |
| 14168 | } |
| 14169 | connection_.MaybeSendConnectionIdToClient(); |
| 14170 | } |
| 14171 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14172 | TEST_P(QuicConnectionTest, |
| 14173 | ProbedOnAnotherPathAfterPeerIpAddressChangeAtServer) { |
| 14174 | PathProbeTestInit(Perspective::IS_SERVER); |
| 14175 | if (!connection_.validate_client_address()) { |
| 14176 | return; |
| 14177 | } |
| 14178 | |
| 14179 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 14180 | /*port=*/23456); |
| 14181 | |
| 14182 | // Process a packet with a new peer address will start connection migration. |
| 14183 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 14184 | // IETF QUIC send algorithm should be changed to a different object, so no |
| 14185 | // OnPacketSent() called on the old send algorithm. |
| 14186 | EXPECT_CALL(*send_algorithm_, |
| 14187 | OnPacketSent(_, _, _, _, NO_RETRANSMITTABLE_DATA)) |
| 14188 | .Times(0); |
| 14189 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([=]() { |
| 14190 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 14191 | })); |
| 14192 | QuicFrames frames2; |
| 14193 | frames2.push_back(QuicFrame(frame2_)); |
| 14194 | ProcessFramesPacketWithAddresses(frames2, kSelfAddress, kNewPeerAddress, |
| 14195 | ENCRYPTION_FORWARD_SECURE); |
| 14196 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePathValidated(&connection_)); |
| 14197 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 14198 | |
| 14199 | // Switch to use the mock send algorithm. |
| 14200 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 14201 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 14202 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 14203 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 14204 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 14205 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 14206 | .Times(AnyNumber()) |
| 14207 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 14208 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 14209 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 14210 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 14211 | connection_.SetSendAlgorithm(send_algorithm_); |
| 14212 | |
| 14213 | // Receive probing packet with a newer peer address shouldn't override the |
| 14214 | // on-going path validation. |
| 14215 | const QuicSocketAddress kNewerPeerAddress(QuicIpAddress::Loopback4(), |
| 14216 | /*port=*/34567); |
| 14217 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 14218 | .WillOnce(Invoke([&]() { |
| 14219 | EXPECT_EQ(kNewerPeerAddress, writer_->last_write_peer_address()); |
| 14220 | EXPECT_FALSE(writer_->path_response_frames().empty()); |
| 14221 | EXPECT_TRUE(writer_->path_challenge_frames().empty()); |
| 14222 | })); |
| 14223 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 14224 | QuicFrames frames1; |
| 14225 | frames1.push_back( |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 14226 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14227 | ProcessFramesPacketWithAddresses(frames1, kSelfAddress, kNewerPeerAddress, |
| 14228 | ENCRYPTION_FORWARD_SECURE); |
| 14229 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 14230 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 14231 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePathValidated(&connection_)); |
| 14232 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 14233 | } |
| 14234 | |
| 14235 | TEST_P(QuicConnectionTest, |
| 14236 | PathValidationFailedOnClientDueToLackOfServerConnectionId) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14237 | if (!connection_.connection_migration_use_new_cid()) { |
| 14238 | return; |
| 14239 | } |
| 14240 | PathProbeTestInit(Perspective::IS_CLIENT, |
| 14241 | /*receive_new_server_connection_id=*/false); |
| 14242 | |
| 14243 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Loopback4(), |
| 14244 | /*port=*/34567); |
| 14245 | |
| 14246 | bool success; |
| 14247 | connection_.ValidatePath( |
| 14248 | std::make_unique<TestQuicPathValidationContext>( |
| 14249 | kNewSelfAddress, connection_.peer_address(), writer_.get()), |
| 14250 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 14251 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 14252 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14253 | |
| 14254 | EXPECT_FALSE(success); |
| 14255 | } |
| 14256 | |
| 14257 | TEST_P(QuicConnectionTest, |
| 14258 | PathValidationFailedOnClientDueToLackOfClientConnectionIdTheSecondTime) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14259 | if (!connection_.connection_migration_use_new_cid()) { |
| 14260 | return; |
| 14261 | } |
| 14262 | PathProbeTestInit(Perspective::IS_CLIENT, |
| 14263 | /*receive_new_server_connection_id=*/false); |
| 14264 | SetClientConnectionId(TestConnectionId(1)); |
| 14265 | |
| 14266 | // Make sure server connection ID is available for the 1st validation. |
| 14267 | QuicConnectionId server_cid0 = connection_.connection_id(); |
| 14268 | QuicConnectionId server_cid1 = TestConnectionId(2); |
| 14269 | QuicConnectionId server_cid2 = TestConnectionId(4); |
| 14270 | QuicConnectionId client_cid1; |
| 14271 | QuicNewConnectionIdFrame frame1; |
| 14272 | frame1.connection_id = server_cid1; |
| 14273 | frame1.sequence_number = 1u; |
| 14274 | frame1.retire_prior_to = 0u; |
| 14275 | frame1.stateless_reset_token = |
| 14276 | QuicUtils::GenerateStatelessResetToken(frame1.connection_id); |
| 14277 | connection_.OnNewConnectionIdFrame(frame1); |
| 14278 | const auto* packet_creator = |
| 14279 | QuicConnectionPeer::GetPacketCreator(&connection_); |
| 14280 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), server_cid0); |
| 14281 | |
| 14282 | // Client will issue a new client connection ID to server. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14283 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14284 | .WillOnce(Return(TestConnectionId(456))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14285 | EXPECT_CALL(visitor_, SendNewConnectionId(_)) |
| 14286 | .WillOnce(Invoke([&](const QuicNewConnectionIdFrame& frame) { |
| 14287 | client_cid1 = frame.connection_id; |
| 14288 | })); |
| 14289 | |
| 14290 | const QuicSocketAddress kSelfAddress1(QuicIpAddress::Any4(), 12345); |
| 14291 | ASSERT_NE(kSelfAddress1, connection_.self_address()); |
| 14292 | bool success1; |
| 14293 | connection_.ValidatePath( |
| 14294 | std::make_unique<TestQuicPathValidationContext>( |
| 14295 | kSelfAddress1, connection_.peer_address(), writer_.get()), |
| 14296 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 14297 | &connection_, kSelfAddress1, connection_.peer_address(), &success1), |
| 14298 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14299 | |
| 14300 | // Migrate upon 1st validation success. |
| 14301 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 14302 | ASSERT_TRUE(connection_.MigratePath(kSelfAddress1, connection_.peer_address(), |
| 14303 | &new_writer, /*owns_writer=*/false)); |
| 14304 | QuicConnectionPeer::RetirePeerIssuedConnectionIdsNoLongerOnPath(&connection_); |
| 14305 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 14306 | EXPECT_EQ(default_path->client_connection_id, client_cid1); |
| 14307 | EXPECT_EQ(default_path->server_connection_id, server_cid1); |
| 14308 | EXPECT_EQ(default_path->stateless_reset_token, frame1.stateless_reset_token); |
| 14309 | const auto* alternative_path = |
| 14310 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 14311 | EXPECT_TRUE(alternative_path->client_connection_id.IsEmpty()); |
| 14312 | EXPECT_TRUE(alternative_path->server_connection_id.IsEmpty()); |
| 14313 | EXPECT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 14314 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), server_cid1); |
| 14315 | |
| 14316 | // Client will retire server connection ID on old default_path. |
| 14317 | auto* retire_peer_issued_cid_alarm = |
| 14318 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14319 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14320 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 14321 | retire_peer_issued_cid_alarm->Fire(); |
| 14322 | |
| 14323 | // Another server connection ID is available to client. |
| 14324 | QuicNewConnectionIdFrame frame2; |
| 14325 | frame2.connection_id = server_cid2; |
| 14326 | frame2.sequence_number = 2u; |
| 14327 | frame2.retire_prior_to = 1u; |
| 14328 | frame2.stateless_reset_token = |
| 14329 | QuicUtils::GenerateStatelessResetToken(frame2.connection_id); |
| 14330 | connection_.OnNewConnectionIdFrame(frame2); |
| 14331 | |
| 14332 | const QuicSocketAddress kSelfAddress2(QuicIpAddress::Loopback4(), |
| 14333 | /*port=*/45678); |
| 14334 | bool success2; |
| 14335 | connection_.ValidatePath( |
| 14336 | std::make_unique<TestQuicPathValidationContext>( |
| 14337 | kSelfAddress2, connection_.peer_address(), writer_.get()), |
| 14338 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 14339 | &connection_, kSelfAddress2, connection_.peer_address(), &success2), |
| 14340 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14341 | // Since server does not retire any client connection ID yet, 2nd validation |
| 14342 | // would fail due to lack of client connection ID. |
| 14343 | EXPECT_FALSE(success2); |
| 14344 | } |
| 14345 | |
| 14346 | TEST_P(QuicConnectionTest, ServerConnectionIdRetiredUponPathValidationFailure) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14347 | if (!connection_.connection_migration_use_new_cid()) { |
| 14348 | return; |
| 14349 | } |
| 14350 | PathProbeTestInit(Perspective::IS_CLIENT); |
| 14351 | |
| 14352 | // Make sure server connection ID is available for validation. |
| 14353 | QuicNewConnectionIdFrame frame; |
| 14354 | frame.connection_id = TestConnectionId(2); |
| 14355 | frame.sequence_number = 1u; |
| 14356 | frame.retire_prior_to = 0u; |
| 14357 | frame.stateless_reset_token = |
| 14358 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14359 | connection_.OnNewConnectionIdFrame(frame); |
| 14360 | |
| 14361 | const QuicSocketAddress kNewSelfAddress(QuicIpAddress::Loopback4(), |
| 14362 | /*port=*/34567); |
| 14363 | bool success; |
| 14364 | connection_.ValidatePath( |
| 14365 | std::make_unique<TestQuicPathValidationContext>( |
| 14366 | kNewSelfAddress, connection_.peer_address(), writer_.get()), |
| 14367 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 14368 | &connection_, kNewSelfAddress, connection_.peer_address(), &success), |
| 14369 | PathValidationReason::kReasonUnknown); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14370 | |
| 14371 | auto* path_validator = QuicConnectionPeer::path_validator(&connection_); |
| 14372 | path_validator->CancelPathValidation(); |
| 14373 | QuicConnectionPeer::RetirePeerIssuedConnectionIdsNoLongerOnPath(&connection_); |
| 14374 | EXPECT_FALSE(success); |
| 14375 | const auto* alternative_path = |
| 14376 | QuicConnectionPeer::GetAlternativePath(&connection_); |
| 14377 | EXPECT_TRUE(alternative_path->client_connection_id.IsEmpty()); |
| 14378 | EXPECT_TRUE(alternative_path->server_connection_id.IsEmpty()); |
| 14379 | EXPECT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 14380 | |
| 14381 | // Client will retire server connection ID on alternative_path. |
| 14382 | auto* retire_peer_issued_cid_alarm = |
| 14383 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14384 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14385 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/1u)); |
| 14386 | retire_peer_issued_cid_alarm->Fire(); |
| 14387 | } |
| 14388 | |
| 14389 | TEST_P(QuicConnectionTest, |
| 14390 | MigratePathDirectlyFailedDueToLackOfServerConnectionId) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14391 | if (!connection_.connection_migration_use_new_cid()) { |
| 14392 | return; |
| 14393 | } |
| 14394 | PathProbeTestInit(Perspective::IS_CLIENT, |
| 14395 | /*receive_new_server_connection_id=*/false); |
| 14396 | const QuicSocketAddress kSelfAddress1(QuicIpAddress::Any4(), 12345); |
| 14397 | ASSERT_NE(kSelfAddress1, connection_.self_address()); |
| 14398 | |
| 14399 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 14400 | ASSERT_FALSE(connection_.MigratePath(kSelfAddress1, |
| 14401 | connection_.peer_address(), &new_writer, |
| 14402 | /*owns_writer=*/false)); |
| 14403 | } |
| 14404 | |
| 14405 | TEST_P(QuicConnectionTest, |
| 14406 | MigratePathDirectlyFailedDueToLackOfClientConnectionIdTheSecondTime) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14407 | if (!connection_.connection_migration_use_new_cid()) { |
| 14408 | return; |
| 14409 | } |
| 14410 | PathProbeTestInit(Perspective::IS_CLIENT, |
| 14411 | /*receive_new_server_connection_id=*/false); |
| 14412 | SetClientConnectionId(TestConnectionId(1)); |
| 14413 | |
| 14414 | // Make sure server connection ID is available for the 1st migration. |
| 14415 | QuicNewConnectionIdFrame frame1; |
| 14416 | frame1.connection_id = TestConnectionId(2); |
| 14417 | frame1.sequence_number = 1u; |
| 14418 | frame1.retire_prior_to = 0u; |
| 14419 | frame1.stateless_reset_token = |
| 14420 | QuicUtils::GenerateStatelessResetToken(frame1.connection_id); |
| 14421 | connection_.OnNewConnectionIdFrame(frame1); |
| 14422 | |
| 14423 | // Client will issue a new client connection ID to server. |
| 14424 | QuicConnectionId new_client_connection_id; |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14425 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14426 | .WillOnce(Return(TestConnectionId(456))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14427 | EXPECT_CALL(visitor_, SendNewConnectionId(_)) |
| 14428 | .WillOnce(Invoke([&](const QuicNewConnectionIdFrame& frame) { |
| 14429 | new_client_connection_id = frame.connection_id; |
| 14430 | })); |
| 14431 | |
| 14432 | // 1st migration is successful. |
| 14433 | const QuicSocketAddress kSelfAddress1(QuicIpAddress::Any4(), 12345); |
| 14434 | ASSERT_NE(kSelfAddress1, connection_.self_address()); |
| 14435 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 14436 | ASSERT_TRUE(connection_.MigratePath(kSelfAddress1, connection_.peer_address(), |
| 14437 | &new_writer, |
| 14438 | /*owns_writer=*/false)); |
| 14439 | QuicConnectionPeer::RetirePeerIssuedConnectionIdsNoLongerOnPath(&connection_); |
| 14440 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 14441 | EXPECT_EQ(default_path->client_connection_id, new_client_connection_id); |
| 14442 | EXPECT_EQ(default_path->server_connection_id, frame1.connection_id); |
| 14443 | EXPECT_EQ(default_path->stateless_reset_token, frame1.stateless_reset_token); |
| 14444 | |
| 14445 | // Client will retire server connection ID on old default_path. |
| 14446 | auto* retire_peer_issued_cid_alarm = |
| 14447 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14448 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14449 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 14450 | retire_peer_issued_cid_alarm->Fire(); |
| 14451 | |
| 14452 | // Another server connection ID is available to client. |
| 14453 | QuicNewConnectionIdFrame frame2; |
| 14454 | frame2.connection_id = TestConnectionId(4); |
| 14455 | frame2.sequence_number = 2u; |
| 14456 | frame2.retire_prior_to = 1u; |
| 14457 | frame2.stateless_reset_token = |
| 14458 | QuicUtils::GenerateStatelessResetToken(frame2.connection_id); |
| 14459 | connection_.OnNewConnectionIdFrame(frame2); |
| 14460 | |
| 14461 | // Since server does not retire any client connection ID yet, 2nd migration |
| 14462 | // would fail due to lack of client connection ID. |
| 14463 | const QuicSocketAddress kSelfAddress2(QuicIpAddress::Loopback4(), |
| 14464 | /*port=*/45678); |
| 14465 | auto new_writer2 = std::make_unique<TestPacketWriter>(version(), &clock_, |
| 14466 | Perspective::IS_CLIENT); |
| 14467 | ASSERT_FALSE(connection_.MigratePath( |
| 14468 | kSelfAddress2, connection_.peer_address(), new_writer2.release(), |
| 14469 | /*owns_writer=*/true)); |
| 14470 | } |
| 14471 | |
| 14472 | TEST_P(QuicConnectionTest, |
| 14473 | CloseConnectionAfterReceiveNewConnectionIdFromPeerUsingEmptyCID) { |
| 14474 | if (!version().HasIetfQuicFrames()) { |
| 14475 | return; |
| 14476 | } |
| 14477 | set_perspective(Perspective::IS_SERVER); |
| 14478 | ASSERT_TRUE(connection_.client_connection_id().IsEmpty()); |
| 14479 | |
| 14480 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 14481 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 14482 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 14483 | QuicNewConnectionIdFrame frame; |
| 14484 | frame.sequence_number = 1u; |
| 14485 | frame.connection_id = TestConnectionId(1); |
| 14486 | frame.stateless_reset_token = |
| 14487 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14488 | frame.retire_prior_to = 0u; |
| 14489 | |
| 14490 | EXPECT_FALSE(connection_.OnNewConnectionIdFrame(frame)); |
| 14491 | |
| 14492 | EXPECT_FALSE(connection_.connected()); |
| 14493 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 14494 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 14495 | } |
| 14496 | |
| 14497 | TEST_P(QuicConnectionTest, NewConnectionIdFrameResultsInError) { |
| 14498 | if (!version().HasIetfQuicFrames()) { |
| 14499 | return; |
| 14500 | } |
| 14501 | connection_.CreateConnectionIdManager(); |
| 14502 | ASSERT_FALSE(connection_.connection_id().IsEmpty()); |
| 14503 | |
| 14504 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 14505 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 14506 | QuicNewConnectionIdFrame frame; |
| 14507 | frame.sequence_number = 1u; |
| 14508 | frame.connection_id = connection_id_; // Reuses connection ID casuing error. |
| 14509 | frame.stateless_reset_token = |
| 14510 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14511 | frame.retire_prior_to = 0u; |
| 14512 | |
| 14513 | EXPECT_FALSE(connection_.OnNewConnectionIdFrame(frame)); |
| 14514 | |
| 14515 | EXPECT_FALSE(connection_.connected()); |
| 14516 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 14517 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 14518 | } |
| 14519 | |
| 14520 | TEST_P(QuicConnectionTest, |
| 14521 | ClientRetirePeerIssuedConnectionIdTriggeredByNewConnectionIdFrame) { |
| 14522 | if (!version().HasIetfQuicFrames()) { |
| 14523 | return; |
| 14524 | } |
| 14525 | connection_.CreateConnectionIdManager(); |
| 14526 | |
| 14527 | QuicNewConnectionIdFrame frame; |
| 14528 | frame.sequence_number = 1u; |
| 14529 | frame.connection_id = TestConnectionId(1); |
| 14530 | frame.stateless_reset_token = |
| 14531 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14532 | frame.retire_prior_to = 0u; |
| 14533 | |
| 14534 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14535 | auto* retire_peer_issued_cid_alarm = |
| 14536 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14537 | ASSERT_FALSE(retire_peer_issued_cid_alarm->IsSet()); |
| 14538 | |
| 14539 | frame.sequence_number = 2u; |
| 14540 | frame.connection_id = TestConnectionId(2); |
| 14541 | frame.stateless_reset_token = |
| 14542 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14543 | frame.retire_prior_to = 1u; // CID associated with #1 will be retired. |
| 14544 | |
| 14545 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14546 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14547 | EXPECT_EQ(connection_.connection_id(), connection_id_); |
| 14548 | |
| 14549 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 14550 | retire_peer_issued_cid_alarm->Fire(); |
| 14551 | EXPECT_EQ(connection_.connection_id(), TestConnectionId(2)); |
| 14552 | EXPECT_EQ(connection_.packet_creator().GetDestinationConnectionId(), |
| 14553 | TestConnectionId(2)); |
| 14554 | } |
| 14555 | |
| 14556 | TEST_P(QuicConnectionTest, |
| 14557 | ServerRetirePeerIssuedConnectionIdTriggeredByNewConnectionIdFrame) { |
| 14558 | if (!version().HasIetfQuicFrames()) { |
| 14559 | return; |
| 14560 | } |
| 14561 | set_perspective(Perspective::IS_SERVER); |
| 14562 | SetClientConnectionId(TestConnectionId(0)); |
| 14563 | |
| 14564 | QuicNewConnectionIdFrame frame; |
| 14565 | frame.sequence_number = 1u; |
| 14566 | frame.connection_id = TestConnectionId(1); |
| 14567 | frame.stateless_reset_token = |
| 14568 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14569 | frame.retire_prior_to = 0u; |
| 14570 | |
| 14571 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14572 | auto* retire_peer_issued_cid_alarm = |
| 14573 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14574 | ASSERT_FALSE(retire_peer_issued_cid_alarm->IsSet()); |
| 14575 | |
| 14576 | frame.sequence_number = 2u; |
| 14577 | frame.connection_id = TestConnectionId(2); |
| 14578 | frame.stateless_reset_token = |
| 14579 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14580 | frame.retire_prior_to = 1u; // CID associated with #1 will be retired. |
| 14581 | |
| 14582 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14583 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14584 | EXPECT_EQ(connection_.client_connection_id(), TestConnectionId(0)); |
| 14585 | |
| 14586 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 14587 | retire_peer_issued_cid_alarm->Fire(); |
| 14588 | EXPECT_EQ(connection_.client_connection_id(), TestConnectionId(2)); |
| 14589 | EXPECT_EQ(connection_.packet_creator().GetDestinationConnectionId(), |
| 14590 | TestConnectionId(2)); |
| 14591 | } |
| 14592 | |
| 14593 | TEST_P( |
| 14594 | QuicConnectionTest, |
| 14595 | ReplacePeerIssuedConnectionIdOnBothPathsTriggeredByNewConnectionIdFrame) { |
danzh | af2e52a | 2022-04-20 07:37:03 -0700 | [diff] [blame] | 14596 | if (!version().HasIetfQuicFrames()) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14597 | return; |
| 14598 | } |
| 14599 | PathProbeTestInit(Perspective::IS_SERVER); |
| 14600 | SetClientConnectionId(TestConnectionId(0)); |
| 14601 | |
| 14602 | // Populate alternative_path_ with probing packet. |
| 14603 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 14604 | |
| 14605 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 14606 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 14607 | probing_packet->encrypted_length), |
| 14608 | clock_.Now())); |
| 14609 | QuicIpAddress new_host; |
| 14610 | new_host.FromString("1.1.1.1"); |
| 14611 | ProcessReceivedPacket(kSelfAddress, |
| 14612 | QuicSocketAddress(new_host, /*port=*/23456), *received); |
| 14613 | |
| 14614 | EXPECT_EQ( |
| 14615 | TestConnectionId(0), |
| 14616 | QuicConnectionPeer::GetClientConnectionIdOnAlternativePath(&connection_)); |
| 14617 | |
| 14618 | QuicNewConnectionIdFrame frame; |
| 14619 | frame.sequence_number = 1u; |
| 14620 | frame.connection_id = TestConnectionId(1); |
| 14621 | frame.stateless_reset_token = |
| 14622 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14623 | frame.retire_prior_to = 0u; |
| 14624 | |
| 14625 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14626 | auto* retire_peer_issued_cid_alarm = |
| 14627 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 14628 | ASSERT_FALSE(retire_peer_issued_cid_alarm->IsSet()); |
| 14629 | |
| 14630 | frame.sequence_number = 2u; |
| 14631 | frame.connection_id = TestConnectionId(2); |
| 14632 | frame.stateless_reset_token = |
| 14633 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14634 | frame.retire_prior_to = 1u; // CID associated with #1 will be retired. |
| 14635 | |
| 14636 | EXPECT_TRUE(connection_.OnNewConnectionIdFrame(frame)); |
| 14637 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 14638 | EXPECT_EQ(connection_.client_connection_id(), TestConnectionId(0)); |
| 14639 | |
| 14640 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 14641 | retire_peer_issued_cid_alarm->Fire(); |
| 14642 | EXPECT_EQ(connection_.client_connection_id(), TestConnectionId(2)); |
| 14643 | EXPECT_EQ(connection_.packet_creator().GetDestinationConnectionId(), |
| 14644 | TestConnectionId(2)); |
| 14645 | // Clean up alternative path connection ID. |
| 14646 | EXPECT_EQ( |
| 14647 | TestConnectionId(2), |
| 14648 | QuicConnectionPeer::GetClientConnectionIdOnAlternativePath(&connection_)); |
| 14649 | } |
| 14650 | |
| 14651 | TEST_P(QuicConnectionTest, |
| 14652 | CloseConnectionAfterReceiveRetireConnectionIdWhenNoCIDIssued) { |
| 14653 | if (!version().HasIetfQuicFrames() || |
| 14654 | !connection_.connection_migration_use_new_cid()) { |
| 14655 | return; |
| 14656 | } |
| 14657 | set_perspective(Perspective::IS_SERVER); |
| 14658 | |
| 14659 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 14660 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 14661 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 14662 | QuicRetireConnectionIdFrame frame; |
| 14663 | frame.sequence_number = 1u; |
| 14664 | |
| 14665 | EXPECT_FALSE(connection_.OnRetireConnectionIdFrame(frame)); |
| 14666 | |
| 14667 | EXPECT_FALSE(connection_.connected()); |
| 14668 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 14669 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 14670 | } |
| 14671 | |
| 14672 | TEST_P(QuicConnectionTest, RetireConnectionIdFrameResultsInError) { |
| 14673 | if (!version().HasIetfQuicFrames() || |
| 14674 | !connection_.connection_migration_use_new_cid()) { |
| 14675 | return; |
| 14676 | } |
| 14677 | set_perspective(Perspective::IS_SERVER); |
| 14678 | connection_.CreateConnectionIdManager(); |
| 14679 | |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14680 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14681 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14682 | .WillOnce(Return(TestConnectionId(456))); |
| 14683 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14684 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14685 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 14686 | connection_.MaybeSendConnectionIdToClient(); |
| 14687 | |
| 14688 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 14689 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 14690 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 14691 | QuicRetireConnectionIdFrame frame; |
| 14692 | frame.sequence_number = 2u; // The corresponding ID is never issued. |
| 14693 | |
| 14694 | EXPECT_FALSE(connection_.OnRetireConnectionIdFrame(frame)); |
| 14695 | |
| 14696 | EXPECT_FALSE(connection_.connected()); |
| 14697 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 14698 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 14699 | } |
| 14700 | |
| 14701 | TEST_P(QuicConnectionTest, |
| 14702 | ServerRetireSelfIssuedConnectionIdWithoutSendingNewConnectionIdBefore) { |
| 14703 | if (!version().HasIetfQuicFrames()) { |
| 14704 | return; |
| 14705 | } |
| 14706 | set_perspective(Perspective::IS_SERVER); |
| 14707 | connection_.CreateConnectionIdManager(); |
| 14708 | |
| 14709 | auto* retire_self_issued_cid_alarm = |
| 14710 | connection_.GetRetireSelfIssuedConnectionIdAlarm(); |
| 14711 | ASSERT_FALSE(retire_self_issued_cid_alarm->IsSet()); |
| 14712 | |
| 14713 | QuicConnectionId cid0 = connection_id_; |
| 14714 | QuicRetireConnectionIdFrame frame; |
| 14715 | frame.sequence_number = 0u; |
| 14716 | if (connection_.connection_migration_use_new_cid()) { |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14717 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14718 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(cid0)) |
| 14719 | .WillOnce(Return(TestConnectionId(456))); |
| 14720 | EXPECT_CALL(connection_id_generator_, |
| 14721 | GenerateNextConnectionId(TestConnectionId(456))) |
| 14722 | .WillOnce(Return(TestConnectionId(789))); |
| 14723 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14724 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
| 14725 | .Times(2) |
| 14726 | .WillRepeatedly(Return(true)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14727 | EXPECT_CALL(visitor_, SendNewConnectionId(_)).Times(2); |
| 14728 | } |
| 14729 | EXPECT_TRUE(connection_.OnRetireConnectionIdFrame(frame)); |
| 14730 | } |
| 14731 | |
| 14732 | TEST_P(QuicConnectionTest, ServerRetireSelfIssuedConnectionId) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14733 | if (!connection_.connection_migration_use_new_cid()) { |
| 14734 | return; |
| 14735 | } |
| 14736 | set_perspective(Perspective::IS_SERVER); |
| 14737 | connection_.CreateConnectionIdManager(); |
| 14738 | QuicConnectionId recorded_cid; |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14739 | auto cid_recorder = [&recorded_cid](const QuicConnectionId& cid) -> bool { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14740 | recorded_cid = cid; |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14741 | return true; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14742 | }; |
| 14743 | QuicConnectionId cid0 = connection_id_; |
| 14744 | QuicConnectionId cid1; |
| 14745 | QuicConnectionId cid2; |
| 14746 | EXPECT_EQ(connection_.connection_id(), cid0); |
| 14747 | EXPECT_EQ(connection_.GetOneActiveServerConnectionId(), cid0); |
| 14748 | |
| 14749 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14750 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14751 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14752 | .WillOnce(Return(TestConnectionId(456))); |
| 14753 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14754 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14755 | .WillOnce(Invoke(cid_recorder)); |
| 14756 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 14757 | connection_.MaybeSendConnectionIdToClient(); |
| 14758 | cid1 = recorded_cid; |
| 14759 | |
| 14760 | auto* retire_self_issued_cid_alarm = |
| 14761 | connection_.GetRetireSelfIssuedConnectionIdAlarm(); |
| 14762 | ASSERT_FALSE(retire_self_issued_cid_alarm->IsSet()); |
| 14763 | |
| 14764 | // Generate three packets with different connection IDs that will arrive out |
| 14765 | // of order (2, 1, 3) later. |
| 14766 | char buffers[3][kMaxOutgoingPacketSize]; |
| 14767 | // Destination connection ID of packet1 is cid0. |
| 14768 | auto packet1 = |
| 14769 | ConstructPacket({QuicFrame(QuicPingFrame())}, ENCRYPTION_FORWARD_SECURE, |
| 14770 | buffers[0], kMaxOutgoingPacketSize); |
| 14771 | peer_creator_.SetServerConnectionId(cid1); |
| 14772 | auto retire_cid_frame = std::make_unique<QuicRetireConnectionIdFrame>(); |
| 14773 | retire_cid_frame->sequence_number = 0u; |
| 14774 | // Destination connection ID of packet2 is cid1. |
| 14775 | auto packet2 = ConstructPacket({QuicFrame(retire_cid_frame.release())}, |
| 14776 | ENCRYPTION_FORWARD_SECURE, buffers[1], |
| 14777 | kMaxOutgoingPacketSize); |
| 14778 | // Destination connection ID of packet3 is cid1. |
| 14779 | auto packet3 = |
| 14780 | ConstructPacket({QuicFrame(QuicPingFrame())}, ENCRYPTION_FORWARD_SECURE, |
| 14781 | buffers[2], kMaxOutgoingPacketSize); |
| 14782 | |
| 14783 | // Packet2 with RetireConnectionId frame trigers sending NewConnectionId |
| 14784 | // immediately. |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 14785 | if (!connection_.connection_id().IsEmpty()) { |
martinduke | 605dca2 | 2022-09-01 10:40:19 -0700 | [diff] [blame] | 14786 | EXPECT_CALL(connection_id_generator_, GenerateNextConnectionId(_)) |
| 14787 | .WillOnce(Return(TestConnectionId(456))); |
| 14788 | } |
haoyuewang | ada6b82 | 2022-06-23 13:41:18 -0700 | [diff] [blame] | 14789 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14790 | .WillOnce(Invoke(cid_recorder)); |
| 14791 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 14792 | peer_creator_.SetServerConnectionId(cid1); |
| 14793 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *packet2); |
| 14794 | cid2 = recorded_cid; |
| 14795 | // cid0 is not retired immediately. |
| 14796 | EXPECT_THAT(connection_.GetActiveServerConnectionIds(), |
| 14797 | ElementsAre(cid0, cid1, cid2)); |
| 14798 | ASSERT_TRUE(retire_self_issued_cid_alarm->IsSet()); |
| 14799 | EXPECT_EQ(connection_.connection_id(), cid1); |
| 14800 | EXPECT_TRUE(connection_.GetOneActiveServerConnectionId() == cid0 || |
| 14801 | connection_.GetOneActiveServerConnectionId() == cid1 || |
| 14802 | connection_.GetOneActiveServerConnectionId() == cid2); |
| 14803 | |
| 14804 | // Packet1 updates the connection ID on the default path but not the active |
| 14805 | // connection ID. |
| 14806 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *packet1); |
| 14807 | EXPECT_EQ(connection_.connection_id(), cid0); |
| 14808 | EXPECT_TRUE(connection_.GetOneActiveServerConnectionId() == cid0 || |
| 14809 | connection_.GetOneActiveServerConnectionId() == cid1 || |
| 14810 | connection_.GetOneActiveServerConnectionId() == cid2); |
| 14811 | |
| 14812 | // cid0 is retired when the retire CID alarm fires. |
| 14813 | EXPECT_CALL(visitor_, OnServerConnectionIdRetired(cid0)); |
| 14814 | retire_self_issued_cid_alarm->Fire(); |
| 14815 | EXPECT_THAT(connection_.GetActiveServerConnectionIds(), |
| 14816 | ElementsAre(cid1, cid2)); |
| 14817 | EXPECT_TRUE(connection_.GetOneActiveServerConnectionId() == cid1 || |
| 14818 | connection_.GetOneActiveServerConnectionId() == cid2); |
| 14819 | |
| 14820 | // Packet3 updates the connection ID on the default path. |
| 14821 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *packet3); |
| 14822 | EXPECT_EQ(connection_.connection_id(), cid1); |
| 14823 | EXPECT_TRUE(connection_.GetOneActiveServerConnectionId() == cid1 || |
| 14824 | connection_.GetOneActiveServerConnectionId() == cid2); |
| 14825 | } |
| 14826 | |
| 14827 | TEST_P(QuicConnectionTest, PatchMissingClientConnectionIdOntoAlternativePath) { |
| 14828 | if (!version().HasIetfQuicFrames()) { |
| 14829 | return; |
| 14830 | } |
| 14831 | set_perspective(Perspective::IS_SERVER); |
| 14832 | connection_.CreateConnectionIdManager(); |
| 14833 | connection_.set_client_connection_id(TestConnectionId(1)); |
| 14834 | |
| 14835 | // Set up the state after path probing. |
| 14836 | const auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 14837 | auto* alternative_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 14838 | QuicIpAddress new_host; |
| 14839 | new_host.FromString("12.12.12.12"); |
| 14840 | alternative_path->self_address = default_path->self_address; |
| 14841 | alternative_path->peer_address = QuicSocketAddress(new_host, 12345); |
| 14842 | alternative_path->server_connection_id = TestConnectionId(3); |
| 14843 | ASSERT_TRUE(alternative_path->client_connection_id.IsEmpty()); |
| 14844 | ASSERT_FALSE(alternative_path->stateless_reset_token.has_value()); |
| 14845 | |
| 14846 | QuicNewConnectionIdFrame frame; |
| 14847 | frame.sequence_number = 1u; |
| 14848 | frame.connection_id = TestConnectionId(5); |
| 14849 | frame.stateless_reset_token = |
| 14850 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14851 | frame.retire_prior_to = 0u; |
| 14852 | // New ID is patched onto the alternative path when the needed |
| 14853 | // NEW_CONNECTION_ID frame is received after PATH_CHALLENGE frame. |
| 14854 | connection_.OnNewConnectionIdFrame(frame); |
| 14855 | |
| 14856 | ASSERT_EQ(alternative_path->client_connection_id, frame.connection_id); |
| 14857 | ASSERT_EQ(alternative_path->stateless_reset_token, |
| 14858 | frame.stateless_reset_token); |
| 14859 | } |
| 14860 | |
| 14861 | TEST_P(QuicConnectionTest, PatchMissingClientConnectionIdOntoDefaultPath) { |
| 14862 | if (!version().HasIetfQuicFrames()) { |
| 14863 | return; |
| 14864 | } |
| 14865 | set_perspective(Perspective::IS_SERVER); |
| 14866 | connection_.CreateConnectionIdManager(); |
| 14867 | connection_.set_client_connection_id(TestConnectionId(1)); |
| 14868 | |
| 14869 | // Set up the state after peer migration without probing. |
| 14870 | auto* default_path = QuicConnectionPeer::GetDefaultPath(&connection_); |
| 14871 | auto* alternative_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 14872 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 14873 | *alternative_path = std::move(*default_path); |
| 14874 | QuicIpAddress new_host; |
| 14875 | new_host.FromString("12.12.12.12"); |
| 14876 | default_path->self_address = default_path->self_address; |
| 14877 | default_path->peer_address = QuicSocketAddress(new_host, 12345); |
| 14878 | default_path->server_connection_id = TestConnectionId(3); |
| 14879 | packet_creator->SetDefaultPeerAddress(default_path->peer_address); |
| 14880 | packet_creator->SetServerConnectionId(default_path->server_connection_id); |
| 14881 | packet_creator->SetClientConnectionId(default_path->client_connection_id); |
| 14882 | |
| 14883 | ASSERT_FALSE(default_path->validated); |
| 14884 | ASSERT_TRUE(default_path->client_connection_id.IsEmpty()); |
| 14885 | ASSERT_FALSE(default_path->stateless_reset_token.has_value()); |
| 14886 | |
| 14887 | QuicNewConnectionIdFrame frame; |
| 14888 | frame.sequence_number = 1u; |
| 14889 | frame.connection_id = TestConnectionId(5); |
| 14890 | frame.stateless_reset_token = |
| 14891 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 14892 | frame.retire_prior_to = 0u; |
| 14893 | // New ID is patched onto the default path when the needed |
| 14894 | // NEW_CONNECTION_ID frame is received after PATH_CHALLENGE frame. |
| 14895 | connection_.OnNewConnectionIdFrame(frame); |
| 14896 | |
| 14897 | ASSERT_EQ(default_path->client_connection_id, frame.connection_id); |
| 14898 | ASSERT_EQ(default_path->stateless_reset_token, frame.stateless_reset_token); |
| 14899 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), frame.connection_id); |
| 14900 | } |
| 14901 | |
| 14902 | TEST_P(QuicConnectionTest, ShouldGeneratePacketBlockedByMissingConnectionId) { |
| 14903 | if (!version().HasIetfQuicFrames()) { |
| 14904 | return; |
| 14905 | } |
| 14906 | set_perspective(Perspective::IS_SERVER); |
| 14907 | connection_.set_client_connection_id(TestConnectionId(1)); |
| 14908 | connection_.CreateConnectionIdManager(); |
| 14909 | if (version().SupportsAntiAmplificationLimit()) { |
| 14910 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 14911 | } |
| 14912 | |
| 14913 | ASSERT_TRUE( |
| 14914 | connection_.ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, NOT_HANDSHAKE)); |
| 14915 | |
| 14916 | QuicPacketCreator* packet_creator = |
| 14917 | QuicConnectionPeer::GetPacketCreator(&connection_); |
| 14918 | QuicIpAddress peer_host1; |
| 14919 | peer_host1.FromString("12.12.12.12"); |
| 14920 | QuicSocketAddress peer_address1(peer_host1, 1235); |
| 14921 | |
| 14922 | { |
| 14923 | // No connection ID is available as context is created without any. |
| 14924 | QuicPacketCreator::ScopedPeerAddressContext context( |
| 14925 | packet_creator, peer_address1, EmptyQuicConnectionId(), |
| 14926 | EmptyQuicConnectionId(), |
| 14927 | /*update_connection_id=*/true); |
| 14928 | ASSERT_FALSE(connection_.ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 14929 | NOT_HANDSHAKE)); |
| 14930 | } |
| 14931 | ASSERT_TRUE( |
| 14932 | connection_.ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, NOT_HANDSHAKE)); |
| 14933 | } |
| 14934 | |
| 14935 | // Regression test for b/182571515 |
| 14936 | TEST_P(QuicConnectionTest, LostDataThenGetAcknowledged) { |
| 14937 | set_perspective(Perspective::IS_SERVER); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 14938 | if (!connection_.validate_client_address() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 14939 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14940 | return; |
| 14941 | } |
| 14942 | |
| 14943 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 14944 | if (version().SupportsAntiAmplificationLimit()) { |
| 14945 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 14946 | } |
| 14947 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 14948 | // Discard INITIAL key. |
| 14949 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 14950 | connection_.NeuterUnencryptedPackets(); |
| 14951 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 14952 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 14953 | |
| 14954 | QuicPacketNumber last_packet; |
| 14955 | // Send packets 1 to 4. |
| 14956 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 14957 | SendStreamDataToPeer(3, "foo", 3, NO_FIN, &last_packet); // Packet 2 |
| 14958 | SendStreamDataToPeer(3, "foo", 6, NO_FIN, &last_packet); // Packet 3 |
| 14959 | SendStreamDataToPeer(3, "foo", 9, NO_FIN, &last_packet); // Packet 4 |
| 14960 | |
| 14961 | // Process a PING packet to set peer address. |
| 14962 | ProcessFramePacket(QuicFrame(QuicPingFrame())); |
| 14963 | |
| 14964 | // Process a packet containing a STREAM_FRAME and an ACK with changed peer |
| 14965 | // address. |
| 14966 | QuicFrames frames; |
| 14967 | frames.push_back(QuicFrame(frame1_)); |
| 14968 | QuicAckFrame ack = InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)}}); |
| 14969 | frames.push_back(QuicFrame(&ack)); |
| 14970 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14971 | // Invoke OnCanWrite. |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14972 | QuicIpAddress ip_address; |
| 14973 | ASSERT_TRUE(ip_address.FromString("127.0.52.223")); |
vasilvv | ac2e30d | 2022-06-02 14:26:59 -0700 | [diff] [blame] | 14974 | EXPECT_QUIC_BUG( |
| 14975 | { |
| 14976 | EXPECT_CALL(visitor_, OnConnectionMigration(_)).Times(1); |
| 14977 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 14978 | .WillOnce(InvokeWithoutArgs(¬ifier_, |
| 14979 | &SimpleSessionNotifier::OnCanWrite)); |
| 14980 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, |
| 14981 | QuicSocketAddress(ip_address, 1000), |
| 14982 | ENCRYPTION_FORWARD_SECURE); |
| 14983 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
| 14984 | |
| 14985 | // Verify stream frame will not be retransmitted. |
| 14986 | EXPECT_TRUE(writer_->stream_frames().empty()); |
| 14987 | }, |
| 14988 | "Try to write mid packet processing"); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 14989 | } |
| 14990 | |
| 14991 | TEST_P(QuicConnectionTest, PtoSendStreamData) { |
| 14992 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 14993 | return; |
| 14994 | } |
| 14995 | set_perspective(Perspective::IS_SERVER); |
| 14996 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 14997 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 14998 | } |
| 14999 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15000 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15001 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 15002 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15003 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15004 | // Send INITIAL 1. |
| 15005 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15006 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15007 | connection_.SetEncrypter( |
| 15008 | ENCRYPTION_HANDSHAKE, |
| 15009 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15010 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15011 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15012 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15013 | // Send HANDSHAKE packets. |
| 15014 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15015 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 15016 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15017 | connection_.SetEncrypter( |
| 15018 | ENCRYPTION_FORWARD_SECURE, |
| 15019 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15020 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15021 | |
| 15022 | // Send half RTT packet with congestion control blocked. |
| 15023 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(false)); |
| 15024 | connection_.SendStreamDataWithString(2, std::string(1500, 'a'), 0, NO_FIN); |
| 15025 | |
| 15026 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15027 | connection_.GetRetransmissionAlarm()->Fire(); |
| 15028 | // Verify INITIAL and HANDSHAKE get retransmitted. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15029 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15030 | } |
| 15031 | |
| 15032 | TEST_P(QuicConnectionTest, SendingZeroRttPacketsDoesNotPostponePTO) { |
| 15033 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 15034 | return; |
| 15035 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15036 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15037 | // Send CHLO. |
| 15038 | connection_.SendCryptoStreamData(); |
| 15039 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15040 | // Install 0-RTT keys. |
| 15041 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 15042 | std::make_unique<TaggingEncrypter>(0x02)); |
| 15043 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15044 | |
| 15045 | // CHLO gets acknowledged after 10ms. |
| 15046 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 15047 | QuicAckFrame frame1 = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15048 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15049 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 15050 | // Verify PTO is still armed since address validation is not finished yet. |
| 15051 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15052 | QuicTime pto_deadline = connection_.GetRetransmissionAlarm()->deadline(); |
| 15053 | |
| 15054 | // Send 0-RTT packet. |
| 15055 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 15056 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 15057 | std::make_unique<TaggingEncrypter>(0x02)); |
| 15058 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15059 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 15060 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15061 | // PTO deadline should be unchanged. |
| 15062 | EXPECT_EQ(pto_deadline, connection_.GetRetransmissionAlarm()->deadline()); |
| 15063 | } |
| 15064 | |
| 15065 | TEST_P(QuicConnectionTest, QueueingUndecryptablePacketsDoesntPostponePTO) { |
| 15066 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 15067 | return; |
| 15068 | } |
| 15069 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 15070 | QuicConfig config; |
| 15071 | config.set_max_undecryptable_packets(3); |
| 15072 | connection_.SetFromConfig(config); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15073 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15074 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
| 15075 | // Send CHLO. |
| 15076 | connection_.SendCryptoStreamData(); |
| 15077 | |
| 15078 | // Send 0-RTT packet. |
| 15079 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 15080 | std::make_unique<TaggingEncrypter>(0x02)); |
| 15081 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15082 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 15083 | |
| 15084 | // CHLO gets acknowledged after 10ms. |
| 15085 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 15086 | QuicAckFrame frame1 = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15087 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15088 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 15089 | // Verify PTO is still armed since address validation is not finished yet. |
| 15090 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15091 | QuicTime pto_deadline = connection_.GetRetransmissionAlarm()->deadline(); |
| 15092 | |
| 15093 | // Receive an undecryptable packets. |
| 15094 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 15095 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 15096 | std::make_unique<TaggingEncrypter>(0xFF)); |
| 15097 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 15098 | // Verify PTO deadline is sooner. |
| 15099 | EXPECT_GT(pto_deadline, connection_.GetRetransmissionAlarm()->deadline()); |
| 15100 | pto_deadline = connection_.GetRetransmissionAlarm()->deadline(); |
| 15101 | |
| 15102 | // PTO fires. |
| 15103 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 15104 | clock_.AdvanceTime(pto_deadline - clock_.ApproximateNow()); |
| 15105 | connection_.GetRetransmissionAlarm()->Fire(); |
| 15106 | // Verify PTO is still armed since address validation is not finished yet. |
| 15107 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15108 | pto_deadline = connection_.GetRetransmissionAlarm()->deadline(); |
| 15109 | |
| 15110 | // Verify PTO deadline does not change. |
| 15111 | ProcessDataPacketAtLevel(4, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
| 15112 | EXPECT_EQ(pto_deadline, connection_.GetRetransmissionAlarm()->deadline()); |
| 15113 | } |
| 15114 | |
| 15115 | TEST_P(QuicConnectionTest, QueueUndecryptableHandshakePackets) { |
| 15116 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 15117 | return; |
| 15118 | } |
| 15119 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 15120 | QuicConfig config; |
| 15121 | config.set_max_undecryptable_packets(3); |
| 15122 | connection_.SetFromConfig(config); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15123 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15124 | connection_.RemoveDecrypter(ENCRYPTION_HANDSHAKE); |
| 15125 | // Send CHLO. |
| 15126 | connection_.SendCryptoStreamData(); |
| 15127 | |
| 15128 | // Send 0-RTT packet. |
| 15129 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 15130 | std::make_unique<TaggingEncrypter>(0x02)); |
| 15131 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15132 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 15133 | EXPECT_EQ(0u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 15134 | |
| 15135 | // Receive an undecryptable handshake packet. |
| 15136 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 15137 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 15138 | std::make_unique<TaggingEncrypter>(0xFF)); |
| 15139 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_HANDSHAKE); |
| 15140 | // Verify this handshake packet gets queued. |
| 15141 | EXPECT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 15142 | } |
| 15143 | |
| 15144 | TEST_P(QuicConnectionTest, PingNotSentAt0RTTLevelWhenInitialAvailable) { |
| 15145 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 15146 | return; |
| 15147 | } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15148 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15149 | // Send CHLO. |
| 15150 | connection_.SendCryptoStreamData(); |
| 15151 | // Send 0-RTT packet. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15152 | connection_.SetEncrypter( |
| 15153 | ENCRYPTION_ZERO_RTT, |
| 15154 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15155 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15156 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 15157 | |
| 15158 | // CHLO gets acknowledged after 10ms. |
| 15159 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 15160 | QuicAckFrame frame1 = InitAckFrame(1); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15161 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15162 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 15163 | // Verify PTO is still armed since address validation is not finished yet. |
| 15164 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15165 | QuicTime pto_deadline = connection_.GetRetransmissionAlarm()->deadline(); |
| 15166 | |
| 15167 | // PTO fires. |
| 15168 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 15169 | clock_.AdvanceTime(pto_deadline - clock_.ApproximateNow()); |
| 15170 | connection_.GetRetransmissionAlarm()->Fire(); |
| 15171 | // Verify the PING gets sent in ENCRYPTION_INITIAL. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15172 | EXPECT_NE(0x02020202u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15173 | } |
| 15174 | |
| 15175 | TEST_P(QuicConnectionTest, AckElicitingFrames) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15176 | if (!version().HasIetfQuicFrames() || |
| 15177 | !connection_.connection_migration_use_new_cid()) { |
| 15178 | return; |
| 15179 | } |
martinduke | 08e3ff8 | 2022-10-18 09:06:26 -0700 | [diff] [blame] | 15180 | EXPECT_CALL(connection_id_generator_, |
| 15181 | GenerateNextConnectionId(TestConnectionId(12))) |
| 15182 | .WillOnce(Return(TestConnectionId(456))); |
| 15183 | EXPECT_CALL(connection_id_generator_, |
| 15184 | GenerateNextConnectionId(TestConnectionId(456))) |
| 15185 | .WillOnce(Return(TestConnectionId(789))); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15186 | EXPECT_CALL(visitor_, SendNewConnectionId(_)).Times(2); |
| 15187 | EXPECT_CALL(visitor_, OnRstStream(_)); |
| 15188 | EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); |
| 15189 | EXPECT_CALL(visitor_, OnBlockedFrame(_)); |
| 15190 | EXPECT_CALL(visitor_, OnHandshakeDoneReceived()); |
| 15191 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15192 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15193 | EXPECT_CALL(visitor_, OnMaxStreamsFrame(_)); |
| 15194 | EXPECT_CALL(visitor_, OnStreamsBlockedFrame(_)); |
| 15195 | EXPECT_CALL(visitor_, OnStopSendingFrame(_)); |
| 15196 | EXPECT_CALL(visitor_, OnMessageReceived("")); |
| 15197 | EXPECT_CALL(visitor_, OnNewTokenReceived("")); |
| 15198 | |
| 15199 | SetClientConnectionId(TestConnectionId(12)); |
| 15200 | connection_.CreateConnectionIdManager(); |
| 15201 | QuicConnectionPeer::GetSelfIssuedConnectionIdManager(&connection_) |
| 15202 | ->MaybeSendNewConnectionIds(); |
| 15203 | connection_.set_can_receive_ack_frequency_frame(); |
| 15204 | |
| 15205 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 15206 | QuicRstStreamFrame rst_stream_frame; |
| 15207 | QuicWindowUpdateFrame window_update_frame; |
| 15208 | QuicPathChallengeFrame path_challenge_frame; |
| 15209 | QuicNewConnectionIdFrame new_connection_id_frame; |
| 15210 | QuicRetireConnectionIdFrame retire_connection_id_frame; |
| 15211 | retire_connection_id_frame.sequence_number = 1u; |
| 15212 | QuicStopSendingFrame stop_sending_frame; |
| 15213 | QuicPathResponseFrame path_response_frame; |
| 15214 | QuicMessageFrame message_frame; |
| 15215 | QuicNewTokenFrame new_token_frame; |
| 15216 | QuicAckFrequencyFrame ack_frequency_frame; |
| 15217 | QuicBlockedFrame blocked_frame; |
| 15218 | size_t packet_number = 1; |
| 15219 | |
| 15220 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15221 | |
| 15222 | for (uint8_t i = 0; i < NUM_FRAME_TYPES; ++i) { |
| 15223 | QuicFrameType frame_type = static_cast<QuicFrameType>(i); |
| 15224 | bool skipped = false; |
| 15225 | QuicFrame frame; |
| 15226 | QuicFrames frames; |
| 15227 | // Add some padding to fullfill the min size requirement of header |
| 15228 | // protection. |
| 15229 | frames.push_back(QuicFrame(QuicPaddingFrame(10))); |
| 15230 | switch (frame_type) { |
| 15231 | case PADDING_FRAME: |
| 15232 | frame = QuicFrame(QuicPaddingFrame(10)); |
| 15233 | break; |
| 15234 | case MTU_DISCOVERY_FRAME: |
| 15235 | frame = QuicFrame(QuicMtuDiscoveryFrame()); |
| 15236 | break; |
| 15237 | case PING_FRAME: |
| 15238 | frame = QuicFrame(QuicPingFrame()); |
| 15239 | break; |
| 15240 | case MAX_STREAMS_FRAME: |
| 15241 | frame = QuicFrame(QuicMaxStreamsFrame()); |
| 15242 | break; |
| 15243 | case STOP_WAITING_FRAME: |
| 15244 | // Not supported. |
| 15245 | skipped = true; |
| 15246 | break; |
| 15247 | case STREAMS_BLOCKED_FRAME: |
| 15248 | frame = QuicFrame(QuicStreamsBlockedFrame()); |
| 15249 | break; |
| 15250 | case STREAM_FRAME: |
| 15251 | frame = QuicFrame(QuicStreamFrame()); |
| 15252 | break; |
| 15253 | case HANDSHAKE_DONE_FRAME: |
| 15254 | frame = QuicFrame(QuicHandshakeDoneFrame()); |
| 15255 | break; |
| 15256 | case ACK_FRAME: |
| 15257 | frame = QuicFrame(&ack_frame); |
| 15258 | break; |
| 15259 | case RST_STREAM_FRAME: |
| 15260 | frame = QuicFrame(&rst_stream_frame); |
| 15261 | break; |
| 15262 | case CONNECTION_CLOSE_FRAME: |
| 15263 | // Do not test connection close. |
| 15264 | skipped = true; |
| 15265 | break; |
| 15266 | case GOAWAY_FRAME: |
| 15267 | // Does not exist in IETF QUIC. |
| 15268 | skipped = true; |
| 15269 | break; |
| 15270 | case BLOCKED_FRAME: |
| 15271 | frame = QuicFrame(blocked_frame); |
| 15272 | break; |
| 15273 | case WINDOW_UPDATE_FRAME: |
| 15274 | frame = QuicFrame(window_update_frame); |
| 15275 | break; |
| 15276 | case PATH_CHALLENGE_FRAME: |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 15277 | frame = QuicFrame(path_challenge_frame); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15278 | break; |
| 15279 | case STOP_SENDING_FRAME: |
| 15280 | frame = QuicFrame(stop_sending_frame); |
| 15281 | break; |
| 15282 | case NEW_CONNECTION_ID_FRAME: |
| 15283 | frame = QuicFrame(&new_connection_id_frame); |
| 15284 | break; |
| 15285 | case RETIRE_CONNECTION_ID_FRAME: |
| 15286 | frame = QuicFrame(&retire_connection_id_frame); |
| 15287 | break; |
| 15288 | case PATH_RESPONSE_FRAME: |
wub | d0152ca | 2022-04-08 08:26:44 -0700 | [diff] [blame] | 15289 | frame = QuicFrame(path_response_frame); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15290 | break; |
| 15291 | case MESSAGE_FRAME: |
| 15292 | frame = QuicFrame(&message_frame); |
| 15293 | break; |
| 15294 | case CRYPTO_FRAME: |
| 15295 | // CRYPTO_FRAME is ack eliciting is covered by other tests. |
| 15296 | skipped = true; |
| 15297 | break; |
| 15298 | case NEW_TOKEN_FRAME: |
| 15299 | frame = QuicFrame(&new_token_frame); |
| 15300 | break; |
| 15301 | case ACK_FREQUENCY_FRAME: |
| 15302 | frame = QuicFrame(&ack_frequency_frame); |
| 15303 | break; |
| 15304 | case NUM_FRAME_TYPES: |
| 15305 | skipped = true; |
| 15306 | break; |
| 15307 | } |
| 15308 | if (skipped) { |
| 15309 | continue; |
| 15310 | } |
| 15311 | ASSERT_EQ(frame_type, frame.type); |
| 15312 | frames.push_back(frame); |
| 15313 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 15314 | // Process frame. |
| 15315 | ProcessFramesPacketAtLevel(packet_number++, frames, |
| 15316 | ENCRYPTION_FORWARD_SECURE); |
| 15317 | if (QuicUtils::IsAckElicitingFrame(frame_type)) { |
| 15318 | ASSERT_TRUE(connection_.HasPendingAcks()) << frame; |
| 15319 | // Flush ACK. |
| 15320 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
| 15321 | connection_.GetAckAlarm()->Fire(); |
| 15322 | } |
| 15323 | EXPECT_FALSE(connection_.HasPendingAcks()); |
| 15324 | ASSERT_TRUE(connection_.connected()); |
| 15325 | } |
| 15326 | } |
| 15327 | |
| 15328 | TEST_P(QuicConnectionTest, ReceivedChloAndAck) { |
| 15329 | if (!version().HasIetfQuicFrames()) { |
| 15330 | return; |
| 15331 | } |
| 15332 | set_perspective(Perspective::IS_SERVER); |
| 15333 | QuicFrames frames; |
| 15334 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 15335 | frames.push_back(MakeCryptoFrame()); |
| 15336 | frames.push_back(QuicFrame(&ack_frame)); |
| 15337 | |
| 15338 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 15339 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 15340 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 15341 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 15342 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 15343 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 15344 | ENCRYPTION_INITIAL); |
| 15345 | } |
| 15346 | |
| 15347 | // Regression test for b/201643321. |
| 15348 | TEST_P(QuicConnectionTest, FailedToRetransmitShlo) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15349 | if (!version().HasIetfQuicFrames() || |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 15350 | GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15351 | return; |
| 15352 | } |
| 15353 | set_perspective(Perspective::IS_SERVER); |
| 15354 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 15355 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15356 | // Received INITIAL 1. |
| 15357 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15358 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 15359 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15360 | peer_framer_.SetEncrypter( |
| 15361 | ENCRYPTION_ZERO_RTT, |
| 15362 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15363 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15364 | connection_.SetEncrypter( |
| 15365 | ENCRYPTION_HANDSHAKE, |
| 15366 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15367 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15368 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15369 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15370 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 15371 | connection_.SetEncrypter( |
| 15372 | ENCRYPTION_FORWARD_SECURE, |
| 15373 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15374 | // Received ENCRYPTION_ZERO_RTT 1. |
| 15375 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 15376 | { |
| 15377 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15378 | // Send INITIAL 1. |
| 15379 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15380 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15381 | // Send HANDSHAKE 2. |
| 15382 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15383 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15384 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 15385 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15386 | // Send half RTT data to exhaust amplification credit. |
| 15387 | connection_.SendStreamDataWithString(0, std::string(100 * 1024, 'a'), 0, |
| 15388 | NO_FIN); |
| 15389 | } |
| 15390 | // Received INITIAL 2. |
| 15391 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 15392 | ASSERT_TRUE(connection_.HasPendingAcks()); |
| 15393 | // Verify ACK delay is 1ms. |
| 15394 | EXPECT_EQ(clock_.Now() + kAlarmGranularity, |
| 15395 | connection_.GetAckAlarm()->deadline()); |
| 15396 | // ACK is not throttled by amplification limit, and SHLO is bundled. Also |
| 15397 | // HANDSHAKE + 1RTT packets get coalesced. |
fayang | e975389 | 2022-05-17 03:57:11 -0700 | [diff] [blame] | 15398 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15399 | // ACK alarm fires. |
| 15400 | clock_.AdvanceTime(kAlarmGranularity); |
| 15401 | connection_.GetAckAlarm()->Fire(); |
fayang | e975389 | 2022-05-17 03:57:11 -0700 | [diff] [blame] | 15402 | // Verify 1-RTT packet is coalesced. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15403 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15404 | // Only the first packet in the coalesced packet has been processed, |
| 15405 | // verify SHLO is bundled with INITIAL ACK. |
| 15406 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 15407 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 15408 | // Process the coalesced HANDSHAKE packet. |
| 15409 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 15410 | auto packet = writer_->coalesced_packet()->Clone(); |
| 15411 | writer_->framer()->ProcessPacket(*packet); |
| 15412 | EXPECT_EQ(0u, writer_->ack_frames().size()); |
| 15413 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
fayang | e975389 | 2022-05-17 03:57:11 -0700 | [diff] [blame] | 15414 | // Process the coalesced 1-RTT packet. |
| 15415 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 15416 | packet = writer_->coalesced_packet()->Clone(); |
| 15417 | writer_->framer()->ProcessPacket(*packet); |
| 15418 | EXPECT_EQ(0u, writer_->crypto_frames().size()); |
| 15419 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15420 | |
| 15421 | // Received INITIAL 3. |
| 15422 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 15423 | ProcessCryptoPacketAtLevel(3, ENCRYPTION_INITIAL); |
| 15424 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 15425 | } |
| 15426 | |
| 15427 | // Regression test for b/216133388. |
| 15428 | TEST_P(QuicConnectionTest, FailedToConsumeCryptoData) { |
| 15429 | if (!version().HasIetfQuicFrames()) { |
| 15430 | return; |
| 15431 | } |
| 15432 | set_perspective(Perspective::IS_SERVER); |
| 15433 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 15434 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15435 | // Received INITIAL 1. |
| 15436 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15437 | EXPECT_TRUE(connection_.HasPendingAcks()); |
| 15438 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15439 | peer_framer_.SetEncrypter( |
| 15440 | ENCRYPTION_ZERO_RTT, |
| 15441 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
| 15442 | connection_.SetEncrypter( |
| 15443 | ENCRYPTION_HANDSHAKE, |
| 15444 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15445 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15446 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15447 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15448 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 15449 | connection_.SetEncrypter( |
| 15450 | ENCRYPTION_FORWARD_SECURE, |
| 15451 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15452 | // Received ENCRYPTION_ZERO_RTT 1. |
| 15453 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 15454 | { |
| 15455 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15456 | // Send INITIAL 1. |
| 15457 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15458 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15459 | // Send HANDSHAKE 2. |
| 15460 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15461 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15462 | connection_.SendCryptoDataWithString(std::string(200, 'a'), 0, |
| 15463 | ENCRYPTION_HANDSHAKE); |
| 15464 | // Send 1-RTT 3. |
| 15465 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15466 | connection_.SendStreamDataWithString(0, std::string(40, 'a'), 0, NO_FIN); |
| 15467 | } |
| 15468 | // Received HANDSHAKE Ping, hence discard INITIAL keys. |
| 15469 | peer_framer_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 15470 | std::make_unique<TaggingEncrypter>(0x03)); |
| 15471 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 15472 | connection_.NeuterUnencryptedPackets(); |
| 15473 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_HANDSHAKE); |
| 15474 | clock_.AdvanceTime(kAlarmGranularity); |
| 15475 | { |
| 15476 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15477 | // Sending this 1-RTT data would leave the coalescer only have space to |
| 15478 | // accommodate the HANDSHAKE ACK. The crypto data cannot be bundled with the |
| 15479 | // ACK. |
| 15480 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15481 | connection_.SendStreamDataWithString(0, std::string(1395, 'a'), 40, NO_FIN); |
| 15482 | } |
| 15483 | // Verify retransmission alarm is armed. |
| 15484 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15485 | const QuicTime retransmission_time = |
| 15486 | connection_.GetRetransmissionAlarm()->deadline(); |
| 15487 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 15488 | connection_.GetRetransmissionAlarm()->Fire(); |
| 15489 | |
fayang | 4329441 | 2022-04-21 09:22:12 -0700 | [diff] [blame] | 15490 | // Verify the retransmission is a coalesced packet with HANDSHAKE 2 and |
| 15491 | // 1-RTT 3. |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15492 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
fayang | 4329441 | 2022-04-21 09:22:12 -0700 | [diff] [blame] | 15493 | // Only the first packet in the coalesced packet has been processed. |
| 15494 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 15495 | // Process the coalesced 1-RTT packet. |
| 15496 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 15497 | auto packet = writer_->coalesced_packet()->Clone(); |
| 15498 | writer_->framer()->ProcessPacket(*packet); |
| 15499 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 15500 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15501 | // Verify retransmission alarm is still armed. |
| 15502 | ASSERT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15503 | } |
| 15504 | |
| 15505 | TEST_P(QuicConnectionTest, |
| 15506 | RTTSampleDoesNotIncludeQueuingDelayWithPostponedAckProcessing) { |
| 15507 | // An endpoint might postpone the processing of ACK when the corresponding |
| 15508 | // decryption key is not available. This test makes sure the RTT sample does |
| 15509 | // not include the queuing delay. |
| 15510 | if (!version().HasIetfQuicFrames()) { |
| 15511 | return; |
| 15512 | } |
| 15513 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 15514 | QuicConfig config; |
| 15515 | config.set_max_undecryptable_packets(3); |
| 15516 | connection_.SetFromConfig(config); |
| 15517 | |
| 15518 | // 30ms RTT. |
| 15519 | const QuicTime::Delta kTestRTT = QuicTime::Delta::FromMilliseconds(30); |
| 15520 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 15521 | rtt_stats->UpdateRtt(kTestRTT, QuicTime::Delta::Zero(), QuicTime::Zero()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15522 | |
| 15523 | // Send 0-RTT packet. |
| 15524 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15525 | connection_.SetEncrypter( |
| 15526 | ENCRYPTION_ZERO_RTT, |
| 15527 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15528 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 15529 | connection_.SendStreamDataWithString(0, std::string(10, 'a'), 0, FIN); |
| 15530 | |
| 15531 | // Receives 1-RTT ACK for 0-RTT packet after RTT + ack_delay. |
| 15532 | clock_.AdvanceTime( |
| 15533 | kTestRTT + QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs)); |
| 15534 | EXPECT_EQ(0u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15535 | peer_framer_.SetEncrypter( |
| 15536 | ENCRYPTION_FORWARD_SECURE, |
| 15537 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15538 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 15539 | // Peer reported ACK delay. |
| 15540 | ack_frame.ack_delay_time = |
| 15541 | QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs); |
| 15542 | QuicFrames frames; |
| 15543 | frames.push_back(QuicFrame(&ack_frame)); |
| 15544 | QuicPacketHeader header = |
| 15545 | ConstructPacketHeader(30, ENCRYPTION_FORWARD_SECURE); |
| 15546 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 15547 | |
| 15548 | char buffer[kMaxOutgoingPacketSize]; |
| 15549 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 15550 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(30), *packet, buffer, |
| 15551 | kMaxOutgoingPacketSize); |
| 15552 | connection_.ProcessUdpPacket( |
| 15553 | kSelfAddress, kPeerAddress, |
| 15554 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 15555 | if (connection_.GetSendAlarm()->IsSet()) { |
| 15556 | connection_.GetSendAlarm()->Fire(); |
| 15557 | } |
| 15558 | ASSERT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 15559 | |
| 15560 | // Assume 1-RTT decrypter is available after 10ms. |
| 15561 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 15562 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15563 | SetDecrypter( |
| 15564 | ENCRYPTION_FORWARD_SECURE, |
| 15565 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15566 | ASSERT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 15567 | |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15568 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15569 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 15570 | // Verify RTT sample does not include queueing delay. |
| 15571 | EXPECT_EQ(rtt_stats->latest_rtt(), kTestRTT); |
| 15572 | } |
| 15573 | |
| 15574 | // Regression test for b/112480134. |
| 15575 | TEST_P(QuicConnectionTest, NoExtraPaddingInReserializedInitial) { |
| 15576 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 15577 | if (!IsDefaultTestConfiguration() || |
| 15578 | !connection_.version().CanSendCoalescedPackets()) { |
| 15579 | return; |
| 15580 | } |
| 15581 | |
| 15582 | set_perspective(Perspective::IS_SERVER); |
| 15583 | MockQuicConnectionDebugVisitor debug_visitor; |
| 15584 | connection_.set_debug_visitor(&debug_visitor); |
| 15585 | |
| 15586 | uint64_t debug_visitor_sent_count = 0; |
| 15587 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _, _, _, _, _, _)) |
| 15588 | .WillRepeatedly([&]() { debug_visitor_sent_count++; }); |
| 15589 | |
| 15590 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 15591 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15592 | |
| 15593 | // Received INITIAL 1. |
| 15594 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15595 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15596 | peer_framer_.SetEncrypter( |
| 15597 | ENCRYPTION_ZERO_RTT, |
| 15598 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
| 15599 | connection_.SetEncrypter( |
| 15600 | ENCRYPTION_HANDSHAKE, |
| 15601 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15602 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15603 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15604 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15605 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 15606 | connection_.SetEncrypter( |
| 15607 | ENCRYPTION_FORWARD_SECURE, |
| 15608 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15609 | |
| 15610 | // Received ENCRYPTION_ZERO_RTT 2. |
| 15611 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 15612 | |
| 15613 | { |
| 15614 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15615 | // Send INITIAL 1. |
| 15616 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15617 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15618 | // Send HANDSHAKE 2. |
| 15619 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15620 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15621 | connection_.SendCryptoDataWithString(std::string(200, 'a'), 0, |
| 15622 | ENCRYPTION_HANDSHAKE); |
| 15623 | // Send 1-RTT 3. |
| 15624 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15625 | connection_.SendStreamDataWithString(0, std::string(400, 'b'), 0, NO_FIN); |
| 15626 | } |
| 15627 | |
| 15628 | // Arrange the stream data to be sent in response to ENCRYPTION_INITIAL 3. |
| 15629 | const std::string data4(1000, '4'); // Data to send in stream id 4 |
| 15630 | const std::string data8(3000, '8'); // Data to send in stream id 8 |
| 15631 | EXPECT_CALL(visitor_, OnCanWrite()).WillOnce([&]() { |
| 15632 | connection_.producer()->SaveStreamData(4, data4); |
| 15633 | connection_.producer()->SaveStreamData(8, data8); |
| 15634 | |
| 15635 | notifier_.WriteOrBufferData(4, data4.size(), FIN_AND_PADDING); |
| 15636 | |
| 15637 | // This should trigger FlushCoalescedPacket. |
| 15638 | notifier_.WriteOrBufferData(8, data8.size(), FIN); |
| 15639 | }); |
| 15640 | |
| 15641 | QuicByteCount pending_padding_after_serialize_2nd_1rtt_packet = 0; |
| 15642 | QuicPacketCount num_1rtt_packets_serialized = 0; |
| 15643 | EXPECT_CALL(connection_, OnSerializedPacket(_)) |
| 15644 | .WillRepeatedly([&](SerializedPacket packet) { |
| 15645 | if (packet.encryption_level == ENCRYPTION_FORWARD_SECURE) { |
| 15646 | num_1rtt_packets_serialized++; |
| 15647 | if (num_1rtt_packets_serialized == 2) { |
| 15648 | pending_padding_after_serialize_2nd_1rtt_packet = |
| 15649 | connection_.packet_creator().pending_padding_bytes(); |
| 15650 | } |
| 15651 | } |
| 15652 | connection_.QuicConnection::OnSerializedPacket(std::move(packet)); |
| 15653 | }); |
| 15654 | |
| 15655 | // Server receives INITIAL 3, this will serialzie FS 7 (stream 4, stream 8), |
| 15656 | // which will trigger a flush of a coalesced packet consists of INITIAL 4, |
| 15657 | // HS 5 and FS 6 (stream 4). |
wub | 08efde4 | 2022-05-09 12:25:02 -0700 | [diff] [blame] | 15658 | |
| 15659 | // Expect no QUIC_BUG. |
| 15660 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 15661 | EXPECT_EQ( |
| 15662 | debug_visitor_sent_count, |
| 15663 | connection_.sent_packet_manager().GetLargestSentPacket().ToUint64()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15664 | |
| 15665 | // The error only happens if after serializing the second 1RTT packet(pkt #7), |
| 15666 | // the pending padding bytes is non zero. |
| 15667 | EXPECT_GT(pending_padding_after_serialize_2nd_1rtt_packet, 0u); |
| 15668 | EXPECT_TRUE(connection_.connected()); |
| 15669 | } |
| 15670 | |
| 15671 | TEST_P(QuicConnectionTest, ReportedAckDelayIncludesQueuingDelay) { |
| 15672 | if (!version().HasIetfQuicFrames()) { |
| 15673 | return; |
| 15674 | } |
| 15675 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 15676 | QuicConfig config; |
| 15677 | config.set_max_undecryptable_packets(3); |
| 15678 | connection_.SetFromConfig(config); |
| 15679 | |
| 15680 | // Receive 1-RTT ack-eliciting packet while keys are not available. |
| 15681 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15682 | peer_framer_.SetEncrypter( |
| 15683 | ENCRYPTION_FORWARD_SECURE, |
| 15684 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15685 | QuicFrames frames; |
| 15686 | frames.push_back(QuicFrame(QuicPingFrame())); |
| 15687 | frames.push_back(QuicFrame(QuicPaddingFrame(100))); |
| 15688 | QuicPacketHeader header = |
| 15689 | ConstructPacketHeader(30, ENCRYPTION_FORWARD_SECURE); |
| 15690 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 15691 | |
| 15692 | char buffer[kMaxOutgoingPacketSize]; |
| 15693 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 15694 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(30), *packet, buffer, |
| 15695 | kMaxOutgoingPacketSize); |
| 15696 | EXPECT_EQ(0u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 15697 | const QuicTime packet_receipt_time = clock_.Now(); |
| 15698 | connection_.ProcessUdpPacket( |
| 15699 | kSelfAddress, kPeerAddress, |
| 15700 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 15701 | if (connection_.GetSendAlarm()->IsSet()) { |
| 15702 | connection_.GetSendAlarm()->Fire(); |
| 15703 | } |
| 15704 | ASSERT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_)); |
| 15705 | // 1-RTT keys become available after 10ms. |
| 15706 | const QuicTime::Delta kQueuingDelay = QuicTime::Delta::FromMilliseconds(10); |
| 15707 | clock_.AdvanceTime(kQueuingDelay); |
| 15708 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 15709 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15710 | SetDecrypter( |
| 15711 | ENCRYPTION_FORWARD_SECURE, |
| 15712 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15713 | ASSERT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 15714 | |
| 15715 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 15716 | ASSERT_TRUE(connection_.HasPendingAcks()); |
fayang | fea655c | 2022-05-17 08:19:12 -0700 | [diff] [blame] | 15717 | EXPECT_EQ(packet_receipt_time + DefaultDelayedAckTime(), |
| 15718 | connection_.GetAckAlarm()->deadline()); |
| 15719 | clock_.AdvanceTime(packet_receipt_time + DefaultDelayedAckTime() - |
| 15720 | clock_.Now()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15721 | // Fire ACK alarm. |
| 15722 | connection_.GetAckAlarm()->Fire(); |
| 15723 | ASSERT_EQ(1u, writer_->ack_frames().size()); |
fayang | fea655c | 2022-05-17 08:19:12 -0700 | [diff] [blame] | 15724 | // Verify ACK delay time does not include queuing delay. |
| 15725 | EXPECT_EQ(DefaultDelayedAckTime(), writer_->ack_frames()[0].ack_delay_time); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15726 | } |
| 15727 | |
| 15728 | TEST_P(QuicConnectionTest, CoalesceOneRTTPacketWithInitialAndHandshakePackets) { |
| 15729 | if (!version().HasIetfQuicFrames()) { |
| 15730 | return; |
| 15731 | } |
| 15732 | set_perspective(Perspective::IS_SERVER); |
| 15733 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 15734 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15735 | |
| 15736 | // Received INITIAL 1. |
| 15737 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15738 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15739 | peer_framer_.SetEncrypter( |
| 15740 | ENCRYPTION_ZERO_RTT, |
| 15741 | std::make_unique<TaggingEncrypter>(ENCRYPTION_ZERO_RTT)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15742 | |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15743 | connection_.SetEncrypter( |
| 15744 | ENCRYPTION_HANDSHAKE, |
| 15745 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15746 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15747 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15748 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15749 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_ZERO_RTT)); |
| 15750 | connection_.SetEncrypter( |
| 15751 | ENCRYPTION_FORWARD_SECURE, |
| 15752 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15753 | |
| 15754 | // Received ENCRYPTION_ZERO_RTT 2. |
| 15755 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 15756 | |
| 15757 | { |
| 15758 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15759 | // Send INITIAL 1. |
| 15760 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15761 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15762 | // Send HANDSHAKE 2. |
| 15763 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15764 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15765 | connection_.SendCryptoDataWithString(std::string(200, 'a'), 0, |
| 15766 | ENCRYPTION_HANDSHAKE); |
| 15767 | // Send 1-RTT data. |
| 15768 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15769 | connection_.SendStreamDataWithString(0, std::string(2000, 'b'), 0, FIN); |
| 15770 | } |
| 15771 | // Verify coalesced packet [INITIAL 1 + HANDSHAKE 2 + part of 1-RTT data] + |
| 15772 | // rest of 1-RTT data get sent. |
| 15773 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 15774 | |
| 15775 | // Received ENCRYPTION_INITIAL 3. |
| 15776 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_INITIAL); |
| 15777 | |
| 15778 | // Verify a coalesced packet gets sent. |
| 15779 | EXPECT_EQ(3u, writer_->packets_write_attempts()); |
| 15780 | |
| 15781 | // Only the first INITIAL packet has been processed yet. |
| 15782 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 15783 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 15784 | |
| 15785 | // Process HANDSHAKE packet. |
| 15786 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 15787 | auto packet = writer_->coalesced_packet()->Clone(); |
| 15788 | writer_->framer()->ProcessPacket(*packet); |
| 15789 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15790 | // Process 1-RTT packet. |
| 15791 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 15792 | packet = writer_->coalesced_packet()->Clone(); |
| 15793 | writer_->framer()->ProcessPacket(*packet); |
| 15794 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 15795 | } |
| 15796 | |
| 15797 | // Regression test for b/180103273 |
| 15798 | TEST_P(QuicConnectionTest, SendMultipleConnectionCloses) { |
| 15799 | if (!version().HasIetfQuicFrames() || |
| 15800 | !GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 15801 | return; |
| 15802 | } |
| 15803 | set_perspective(Perspective::IS_SERVER); |
| 15804 | // Finish handshake. |
| 15805 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 15806 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15807 | notifier_.NeuterUnencryptedData(); |
| 15808 | connection_.NeuterUnencryptedPackets(); |
| 15809 | connection_.OnHandshakeComplete(); |
| 15810 | connection_.RemoveEncrypter(ENCRYPTION_INITIAL); |
| 15811 | connection_.RemoveEncrypter(ENCRYPTION_HANDSHAKE); |
| 15812 | EXPECT_CALL(visitor_, GetHandshakeState()) |
fayang | 59e518a | 2022-11-29 11:16:45 -0800 | [diff] [blame] | 15813 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15814 | |
| 15815 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 15816 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
bnc | 3000cba | 2022-04-18 12:19:14 -0700 | [diff] [blame] | 15817 | // Verify that BeforeConnectionCloseSent() gets called twice, |
| 15818 | // while OnConnectionClosed() is called only once. |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15819 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()).Times(2); |
| 15820 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 15821 | // Send connection close w/o closing connection. |
| 15822 | QuicConnectionPeer::SendConnectionClosePacket( |
| 15823 | &connection_, INTERNAL_ERROR, QUIC_INTERNAL_ERROR, "internal error"); |
bnc | 3000cba | 2022-04-18 12:19:14 -0700 | [diff] [blame] | 15824 | // Fire blackhole detection alarm. This will invoke |
| 15825 | // SendConnectionClosePacket() a second time. |
| 15826 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15827 | } |
| 15828 | |
| 15829 | // Regression test for b/157895910. |
| 15830 | TEST_P(QuicConnectionTest, EarliestSentTimeNotInitializedWhenPtoFires) { |
| 15831 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 15832 | return; |
| 15833 | } |
| 15834 | set_perspective(Perspective::IS_SERVER); |
| 15835 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 15836 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15837 | |
| 15838 | // Received INITIAL 1. |
| 15839 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15840 | connection_.SetEncrypter( |
| 15841 | ENCRYPTION_HANDSHAKE, |
| 15842 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15843 | SetDecrypter(ENCRYPTION_HANDSHAKE, |
martinduke | 9e0811c | 2022-12-08 20:35:57 -0800 | [diff] [blame] | 15844 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_HANDSHAKE)); |
| 15845 | connection_.SetEncrypter( |
| 15846 | ENCRYPTION_FORWARD_SECURE, |
| 15847 | std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15848 | { |
| 15849 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 15850 | // Send INITIAL 1. |
| 15851 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15852 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_INITIAL); |
| 15853 | // Send HANDSHAKE 2. |
| 15854 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 15855 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 15856 | connection_.SendCryptoDataWithString(std::string(200, 'a'), 0, |
| 15857 | ENCRYPTION_HANDSHAKE); |
| 15858 | // Send half RTT data. |
| 15859 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 15860 | connection_.SendStreamDataWithString(0, std::string(2000, 'b'), 0, FIN); |
| 15861 | } |
| 15862 | |
| 15863 | // Received ACKs for both INITIAL and HANDSHAKE packets. |
martinduke | ba00245 | 2023-03-21 08:10:46 -0700 | [diff] [blame] | 15864 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _, _, _)) |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 15865 | .Times(AnyNumber()); |
| 15866 | QuicFrames frames1; |
| 15867 | QuicAckFrame ack_frame1 = InitAckFrame(1); |
| 15868 | frames1.push_back(QuicFrame(&ack_frame1)); |
| 15869 | |
| 15870 | QuicFrames frames2; |
| 15871 | QuicAckFrame ack_frame2 = |
| 15872 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 15873 | frames2.push_back(QuicFrame(&ack_frame2)); |
| 15874 | ProcessCoalescedPacket( |
| 15875 | {{2, frames1, ENCRYPTION_INITIAL}, {3, frames2, ENCRYPTION_HANDSHAKE}}); |
| 15876 | // Verify PTO is not armed given the only outstanding data is half RTT data. |
| 15877 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15878 | } |
| 15879 | |
wub | 47bb728 | 2022-06-14 09:40:33 -0700 | [diff] [blame] | 15880 | TEST_P(QuicConnectionTest, CalculateNetworkBlackholeDelay) { |
| 15881 | if (!IsDefaultTestConfiguration()) { |
| 15882 | return; |
| 15883 | } |
| 15884 | |
| 15885 | const QuicTime::Delta kOneSec = QuicTime::Delta::FromSeconds(1); |
| 15886 | const QuicTime::Delta kTwoSec = QuicTime::Delta::FromSeconds(2); |
| 15887 | const QuicTime::Delta kFourSec = QuicTime::Delta::FromSeconds(4); |
| 15888 | |
| 15889 | // Normal case: blackhole_delay longer than path_degrading_delay + |
| 15890 | // 2*pto_delay. |
| 15891 | EXPECT_EQ(QuicConnection::CalculateNetworkBlackholeDelay(kFourSec, kOneSec, |
| 15892 | kOneSec), |
| 15893 | kFourSec); |
| 15894 | |
| 15895 | EXPECT_EQ(QuicConnection::CalculateNetworkBlackholeDelay(kFourSec, kOneSec, |
| 15896 | kTwoSec), |
| 15897 | QuicTime::Delta::FromSeconds(5)); |
| 15898 | } |
| 15899 | |
fayang | b225e17 | 2022-06-27 17:45:38 -0700 | [diff] [blame] | 15900 | TEST_P(QuicConnectionTest, FixBytesAccountingForBufferedCoalescedPackets) { |
| 15901 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 15902 | return; |
| 15903 | } |
fayang | b225e17 | 2022-06-27 17:45:38 -0700 | [diff] [blame] | 15904 | // Write is blocked. |
| 15905 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AnyNumber()); |
| 15906 | writer_->SetWriteBlocked(); |
| 15907 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15908 | QuicConnectionPeer::SendPing(&connection_); |
| 15909 | const QuicConnectionStats& stats = connection_.GetStats(); |
fayang | 4e28383 | 2022-09-01 12:47:07 -0700 | [diff] [blame] | 15910 | // Verify padding is accounted. |
| 15911 | EXPECT_EQ(stats.bytes_sent, connection_.max_packet_length()); |
fayang | b225e17 | 2022-06-27 17:45:38 -0700 | [diff] [blame] | 15912 | } |
| 15913 | |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15914 | TEST_P(QuicConnectionTest, StrictAntiAmplificationLimit) { |
| 15915 | if (!connection_.version().SupportsAntiAmplificationLimit()) { |
| 15916 | return; |
| 15917 | } |
| 15918 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(AnyNumber()); |
| 15919 | set_perspective(Perspective::IS_SERVER); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15920 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 15921 | // Verify no data can be sent at the beginning because bytes received is 0. |
| 15922 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 15923 | connection_.SendCryptoDataWithString("foo", 0); |
| 15924 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 15925 | EXPECT_FALSE(connection_.CanWrite(NO_RETRANSMITTABLE_DATA)); |
| 15926 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15927 | |
| 15928 | const size_t anti_amplification_factor = |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 15929 | GetQuicFlag(quic_anti_amplification_factor); |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15930 | // Receives packet 1. |
| 15931 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 15932 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 15933 | .Times(anti_amplification_factor); |
| 15934 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 15935 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 15936 | std::make_unique<TaggingEncrypter>(0x02)); |
| 15937 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 15938 | std::make_unique<TaggingEncrypter>(0x03)); |
| 15939 | |
| 15940 | for (size_t i = 1; i < anti_amplification_factor - 1; ++i) { |
| 15941 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 15942 | } |
| 15943 | // Send an addtion packet with max_packet_size - 1. |
| 15944 | connection_.SetMaxPacketLength(connection_.max_packet_length() - 1); |
| 15945 | connection_.SendCryptoDataWithString("bar", |
| 15946 | (anti_amplification_factor - 1) * 3); |
| 15947 | EXPECT_LT(writer_->total_bytes_written(), |
| 15948 | anti_amplification_factor * |
| 15949 | QuicConnectionPeer::BytesReceivedOnDefaultPath(&connection_)); |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 15950 | if (GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15951 | // 3 connection closes which will be buffered. |
| 15952 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); |
| 15953 | // Verify retransmission alarm is not set. |
| 15954 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15955 | } else { |
| 15956 | // Crypto + 3 connection closes. |
| 15957 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(4); |
| 15958 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15959 | } |
| 15960 | // Try to send another packet with max_packet_size. |
| 15961 | connection_.SetMaxPacketLength(connection_.max_packet_length() + 1); |
| 15962 | connection_.SendCryptoDataWithString("bar", anti_amplification_factor * 3); |
| 15963 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 15964 | // Close connection. |
| 15965 | EXPECT_CALL(visitor_, BeforeConnectionCloseSent()); |
| 15966 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 15967 | connection_.CloseConnection( |
| 15968 | QUIC_INTERNAL_ERROR, "error", |
| 15969 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 15970 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
birenroy | ef68622 | 2022-09-12 11:34:34 -0700 | [diff] [blame] | 15971 | if (GetQuicFlag(quic_enforce_strict_amplification_factor)) { |
fayang | 161ce6e | 2022-07-01 18:02:11 -0700 | [diff] [blame] | 15972 | EXPECT_LT(writer_->total_bytes_written(), |
| 15973 | anti_amplification_factor * |
| 15974 | QuicConnectionPeer::BytesReceivedOnDefaultPath(&connection_)); |
| 15975 | } else { |
| 15976 | EXPECT_LT(writer_->total_bytes_written(), |
| 15977 | (anti_amplification_factor + 2) * |
| 15978 | QuicConnectionPeer::BytesReceivedOnDefaultPath(&connection_)); |
| 15979 | EXPECT_GT(writer_->total_bytes_written(), |
| 15980 | (anti_amplification_factor + 1) * |
| 15981 | QuicConnectionPeer::BytesReceivedOnDefaultPath(&connection_)); |
| 15982 | } |
| 15983 | } |
| 15984 | |
martinduke | 96840a0 | 2022-07-14 07:09:06 -0700 | [diff] [blame] | 15985 | TEST_P(QuicConnectionTest, OriginalConnectionId) { |
| 15986 | set_perspective(Perspective::IS_SERVER); |
| 15987 | EXPECT_FALSE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 15988 | EXPECT_EQ(connection_.GetOriginalDestinationConnectionId(), |
| 15989 | connection_.connection_id()); |
| 15990 | QuicConnectionId original({0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}); |
| 15991 | connection_.SetOriginalDestinationConnectionId(original); |
| 15992 | EXPECT_EQ(original, connection_.GetOriginalDestinationConnectionId()); |
| 15993 | // Send a 1-RTT packet to start the DiscardZeroRttDecryptionKeys timer. |
| 15994 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 15995 | ProcessDataPacketAtLevel(1, false, ENCRYPTION_FORWARD_SECURE); |
martinduke | e6444ef | 2022-09-23 12:32:23 -0700 | [diff] [blame] | 15996 | if (connection_.version().UsesTls()) { |
martinduke | 96840a0 | 2022-07-14 07:09:06 -0700 | [diff] [blame] | 15997 | EXPECT_TRUE(connection_.GetDiscardZeroRttDecryptionKeysAlarm()->IsSet()); |
| 15998 | EXPECT_CALL(visitor_, OnServerConnectionIdRetired(original)); |
| 15999 | connection_.GetDiscardZeroRttDecryptionKeysAlarm()->Fire(); |
| 16000 | EXPECT_EQ(connection_.GetOriginalDestinationConnectionId(), |
| 16001 | connection_.connection_id()); |
| 16002 | } else { |
| 16003 | EXPECT_EQ(connection_.GetOriginalDestinationConnectionId(), original); |
| 16004 | } |
| 16005 | } |
| 16006 | |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16007 | ACTION_P2(InstallKeys, conn, level) { |
| 16008 | uint8_t crypto_input = (level == ENCRYPTION_FORWARD_SECURE) ? 0x03 : 0x02; |
| 16009 | conn->SetEncrypter(level, std::make_unique<TaggingEncrypter>(crypto_input)); |
| 16010 | conn->InstallDecrypter( |
| 16011 | level, std::make_unique<StrictTaggingDecrypter>(crypto_input)); |
| 16012 | conn->SetDefaultEncryptionLevel(level); |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16013 | } |
| 16014 | |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16015 | TEST_P(QuicConnectionTest, ServerConnectionIdChangeWithLateInitial) { |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16016 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16017 | return; |
| 16018 | } |
| 16019 | // Call SetFromConfig so that the undecrypted packet buffer size is |
| 16020 | // initialized above zero. |
| 16021 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(1); |
| 16022 | QuicConfig config; |
| 16023 | connection_.SetFromConfig(config); |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16024 | connection_.RemoveEncrypter(ENCRYPTION_FORWARD_SECURE); |
| 16025 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16026 | |
| 16027 | // Send Client Initial. |
| 16028 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 16029 | connection_.SendCryptoStreamData(); |
| 16030 | |
| 16031 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16032 | // Server Handshake packet with new connection ID is buffered. |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16033 | QuicConnectionId old_id = connection_id_; |
| 16034 | connection_id_ = TestConnectionId(2); |
| 16035 | peer_creator_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 16036 | std::make_unique<TaggingEncrypter>(0x02)); |
| 16037 | ProcessCryptoPacketAtLevel(0, ENCRYPTION_HANDSHAKE); |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16038 | EXPECT_EQ(QuicConnectionPeer::NumUndecryptablePackets(&connection_), 1u); |
| 16039 | EXPECT_EQ(connection_.connection_id(), old_id); |
| 16040 | |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16041 | // Server 1-RTT Packet is buffered. |
| 16042 | peer_creator_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 16043 | std::make_unique<TaggingEncrypter>(0x03)); |
| 16044 | ProcessDataPacket(0); |
| 16045 | EXPECT_EQ(QuicConnectionPeer::NumUndecryptablePackets(&connection_), 2u); |
| 16046 | |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16047 | // Pretend the server Initial packet will yield the Handshake keys. |
| 16048 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 16049 | .Times(2) |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16050 | .WillOnce(InstallKeys(&connection_, ENCRYPTION_HANDSHAKE)) |
| 16051 | .WillOnce(InstallKeys(&connection_, ENCRYPTION_FORWARD_SECURE)); |
| 16052 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16053 | ProcessCryptoPacketAtLevel(0, ENCRYPTION_INITIAL); |
| 16054 | // Two packets processed, connection ID changed. |
| 16055 | EXPECT_EQ(QuicConnectionPeer::NumUndecryptablePackets(&connection_), 0u); |
| 16056 | EXPECT_EQ(connection_.connection_id(), connection_id_); |
| 16057 | } |
| 16058 | |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16059 | TEST_P(QuicConnectionTest, ServerConnectionIdChangeTwiceWithLateInitial) { |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16060 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16061 | return; |
| 16062 | } |
| 16063 | // Call SetFromConfig so that the undecrypted packet buffer size is |
| 16064 | // initialized above zero. |
| 16065 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(1); |
| 16066 | QuicConfig config; |
| 16067 | connection_.SetFromConfig(config); |
| 16068 | |
| 16069 | // Send Client Initial. |
| 16070 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 16071 | connection_.SendCryptoStreamData(); |
| 16072 | |
| 16073 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 16074 | // Server Handshake Packet Arrives with new connection ID. |
| 16075 | QuicConnectionId old_id = connection_id_; |
| 16076 | connection_id_ = TestConnectionId(2); |
| 16077 | peer_creator_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 16078 | std::make_unique<TaggingEncrypter>(0x02)); |
| 16079 | ProcessCryptoPacketAtLevel(0, ENCRYPTION_HANDSHAKE); |
| 16080 | // Packet is buffered. |
| 16081 | EXPECT_EQ(QuicConnectionPeer::NumUndecryptablePackets(&connection_), 1u); |
| 16082 | EXPECT_EQ(connection_.connection_id(), old_id); |
| 16083 | |
| 16084 | // Pretend the server Initial packet will yield the Handshake keys. |
| 16085 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
martinduke | bae2405 | 2022-10-06 15:27:46 -0700 | [diff] [blame] | 16086 | .WillOnce(InstallKeys(&connection_, ENCRYPTION_HANDSHAKE)); |
martinduke | c2a3306 | 2022-09-30 16:04:13 -0700 | [diff] [blame] | 16087 | connection_id_ = TestConnectionId(1); |
| 16088 | ProcessCryptoPacketAtLevel(0, ENCRYPTION_INITIAL); |
| 16089 | // Handshake packet discarded because there's a different connection ID. |
| 16090 | EXPECT_EQ(QuicConnectionPeer::NumUndecryptablePackets(&connection_), 0u); |
| 16091 | EXPECT_EQ(connection_.connection_id(), connection_id_); |
| 16092 | } |
| 16093 | |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16094 | TEST_P(QuicConnectionTest, ClientValidatedServerPreferredAddress) { |
| 16095 | // Test the scenario where the client validates server preferred address by |
| 16096 | // receiving PATH_RESPONSE from server preferred address. |
| 16097 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16098 | return; |
| 16099 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16100 | QuicConfig config; |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16101 | ServerPreferredAddressInit(config); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16102 | const QuicSocketAddress kNewSelfAddress = |
| 16103 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16104 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16105 | const StatelessResetToken kNewStatelessResetToken = |
| 16106 | QuicUtils::GenerateStatelessResetToken(TestConnectionId(17)); |
| 16107 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16108 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16109 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16110 | // Kick off path validation of server preferred address on handshake |
| 16111 | // confirmed. |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16112 | EXPECT_CALL(visitor_, |
| 16113 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16114 | .WillOnce(Invoke([&]() { |
| 16115 | connection_.ValidatePath( |
| 16116 | std::make_unique<TestQuicPathValidationContext>( |
| 16117 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16118 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16119 | &connection_), |
| 16120 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16121 | })); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16122 | connection_.OnHandshakeComplete(); |
| 16123 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16124 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 16125 | &connection_, kNewSelfAddress, kServerPreferredAddress)); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16126 | EXPECT_EQ(TestConnectionId(17), |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16127 | new_writer.last_packet_header().destination_connection_id); |
| 16128 | EXPECT_EQ(kServerPreferredAddress, new_writer.last_write_peer_address()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16129 | |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16130 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16131 | QuicPathFrameBuffer payload = |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16132 | new_writer.path_challenge_frames().front().data_buffer; |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16133 | // Send data packet while path validation is pending. |
| 16134 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16135 | ASSERT_FALSE(writer_->stream_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16136 | // While path validation is pending, packet is sent on default path. |
| 16137 | EXPECT_EQ(TestConnectionId(), |
| 16138 | writer_->last_packet_header().destination_connection_id); |
| 16139 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 16140 | EXPECT_TRUE(connection_.IsValidStatelessResetToken(kTestStatelessResetToken)); |
| 16141 | EXPECT_FALSE(connection_.IsValidStatelessResetToken(kNewStatelessResetToken)); |
| 16142 | |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16143 | // Receive path response from server preferred address. |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16144 | QuicFrames frames; |
| 16145 | frames.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
| 16146 | // Verify send_algorithm gets reset after migration (new sent packet is not |
| 16147 | // updated to exsting send_algorithm_). |
| 16148 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16149 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, |
| 16150 | kServerPreferredAddress, |
| 16151 | ENCRYPTION_FORWARD_SECURE); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16152 | ASSERT_FALSE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16153 | EXPECT_TRUE(QuicConnectionPeer::IsDefaultPath(&connection_, kNewSelfAddress, |
| 16154 | kServerPreferredAddress)); |
| 16155 | ASSERT_FALSE(new_writer.stream_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16156 | // Verify stream data is retransmitted on new path. |
| 16157 | EXPECT_EQ(TestConnectionId(17), |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16158 | new_writer.last_packet_header().destination_connection_id); |
| 16159 | EXPECT_EQ(kServerPreferredAddress, new_writer.last_write_peer_address()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16160 | // Verify stateless reset token gets changed. |
| 16161 | EXPECT_FALSE( |
| 16162 | connection_.IsValidStatelessResetToken(kTestStatelessResetToken)); |
| 16163 | EXPECT_TRUE(connection_.IsValidStatelessResetToken(kNewStatelessResetToken)); |
| 16164 | |
| 16165 | auto* retire_peer_issued_cid_alarm = |
| 16166 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16167 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16168 | // Verify client retires connection ID with sequence number 0. |
| 16169 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 16170 | retire_peer_issued_cid_alarm->Fire(); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16171 | EXPECT_TRUE(connection_.GetStats().server_preferred_address_validated); |
| 16172 | EXPECT_FALSE( |
| 16173 | connection_.GetStats().failed_to_validate_server_preferred_address); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16174 | } |
| 16175 | |
| 16176 | TEST_P(QuicConnectionTest, ClientValidatedServerPreferredAddress2) { |
| 16177 | // Test the scenario where the client validates server preferred address by |
| 16178 | // receiving PATH_RESPONSE from original server address. |
| 16179 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16180 | return; |
| 16181 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16182 | QuicConfig config; |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16183 | ServerPreferredAddressInit(config); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16184 | const QuicSocketAddress kNewSelfAddress = |
| 16185 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16186 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16187 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16188 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16189 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16190 | // Kick off path validation of server preferred address on handshake |
| 16191 | // confirmed. |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16192 | EXPECT_CALL(visitor_, |
| 16193 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16194 | .WillOnce(Invoke([&]() { |
| 16195 | connection_.ValidatePath( |
| 16196 | std::make_unique<TestQuicPathValidationContext>( |
| 16197 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16198 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16199 | &connection_), |
| 16200 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16201 | })); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16202 | connection_.OnHandshakeComplete(); |
| 16203 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16204 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16205 | QuicPathFrameBuffer payload = |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16206 | new_writer.path_challenge_frames().front().data_buffer; |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16207 | // Send data packet while path validation is pending. |
| 16208 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16209 | ASSERT_FALSE(writer_->stream_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16210 | EXPECT_EQ(TestConnectionId(), |
| 16211 | writer_->last_packet_header().destination_connection_id); |
| 16212 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 16213 | |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16214 | // Receive path response from original server address. |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16215 | QuicFrames frames; |
| 16216 | frames.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16217 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16218 | ENCRYPTION_FORWARD_SECURE); |
| 16219 | ASSERT_FALSE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16220 | ASSERT_FALSE(new_writer.stream_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16221 | // Verify stream data is retransmitted on new path. |
| 16222 | EXPECT_EQ(TestConnectionId(17), |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16223 | new_writer.last_packet_header().destination_connection_id); |
| 16224 | EXPECT_EQ(kServerPreferredAddress, new_writer.last_write_peer_address()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16225 | |
| 16226 | auto* retire_peer_issued_cid_alarm = |
| 16227 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16228 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16229 | // Verify client retires connection ID with sequence number 0. |
| 16230 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 16231 | retire_peer_issued_cid_alarm->Fire(); |
| 16232 | |
| 16233 | // Verify another packet from original server address gets processed. |
| 16234 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 16235 | frames.clear(); |
| 16236 | frames.push_back(QuicFrame(frame1_)); |
| 16237 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kPeerAddress, |
| 16238 | ENCRYPTION_FORWARD_SECURE); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16239 | EXPECT_TRUE(connection_.GetStats().server_preferred_address_validated); |
| 16240 | EXPECT_FALSE( |
| 16241 | connection_.GetStats().failed_to_validate_server_preferred_address); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16242 | } |
| 16243 | |
| 16244 | TEST_P(QuicConnectionTest, ClientFailedToValidateServerPreferredAddress) { |
| 16245 | // Test the scenario where the client fails to validate server preferred |
| 16246 | // address. |
| 16247 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16248 | return; |
| 16249 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16250 | QuicConfig config; |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16251 | ServerPreferredAddressInit(config); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16252 | const QuicSocketAddress kNewSelfAddress = |
| 16253 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16254 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16255 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16256 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16257 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16258 | // Kick off path validation of server preferred address on handshake |
| 16259 | // confirmed. |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16260 | EXPECT_CALL(visitor_, |
| 16261 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16262 | .WillOnce(Invoke([&]() { |
| 16263 | connection_.ValidatePath( |
| 16264 | std::make_unique<TestQuicPathValidationContext>( |
| 16265 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16266 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16267 | &connection_), |
| 16268 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16269 | })); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16270 | connection_.OnHandshakeComplete(); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16271 | EXPECT_TRUE(connection_.IsValidatingServerPreferredAddress()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16272 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 16273 | &connection_, kNewSelfAddress, kServerPreferredAddress)); |
| 16274 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16275 | |
| 16276 | // Receive mismatched path challenge from original server address. |
| 16277 | QuicFrames frames; |
| 16278 | frames.push_back( |
| 16279 | QuicFrame(QuicPathResponseFrame(99, {0, 1, 2, 3, 4, 5, 6, 7}))); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16280 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16281 | ENCRYPTION_FORWARD_SECURE); |
| 16282 | ASSERT_TRUE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16283 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 16284 | &connection_, kNewSelfAddress, kServerPreferredAddress)); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16285 | |
| 16286 | // Simluate path validation times out. |
| 16287 | for (size_t i = 0; i < QuicPathValidator::kMaxRetryTimes + 1; ++i) { |
| 16288 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 16289 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 16290 | QuicPathValidatorPeer::retry_timer( |
| 16291 | QuicConnectionPeer::path_validator(&connection_))) |
| 16292 | ->Fire(); |
| 16293 | } |
| 16294 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16295 | EXPECT_FALSE(QuicConnectionPeer::IsAlternativePath( |
| 16296 | &connection_, kNewSelfAddress, kServerPreferredAddress)); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16297 | // Verify stream data is sent on the default path. |
| 16298 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16299 | ASSERT_FALSE(writer_->stream_frames().empty()); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16300 | EXPECT_EQ(TestConnectionId(), |
| 16301 | writer_->last_packet_header().destination_connection_id); |
| 16302 | EXPECT_EQ(kPeerAddress, writer_->last_write_peer_address()); |
| 16303 | |
| 16304 | auto* retire_peer_issued_cid_alarm = |
| 16305 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16306 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16307 | // Verify client retires connection ID with sequence number 1. |
| 16308 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/1u)); |
| 16309 | retire_peer_issued_cid_alarm->Fire(); |
| 16310 | EXPECT_TRUE(connection_.IsValidStatelessResetToken(kTestStatelessResetToken)); |
fayang | 37765f6 | 2022-12-27 17:49:13 -0800 | [diff] [blame] | 16311 | EXPECT_FALSE(connection_.GetStats().server_preferred_address_validated); |
| 16312 | EXPECT_TRUE( |
| 16313 | connection_.GetStats().failed_to_validate_server_preferred_address); |
fayang | dbd6a30 | 2022-12-21 16:24:27 -0800 | [diff] [blame] | 16314 | } |
| 16315 | |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16316 | TEST_P(QuicConnectionTest, OptimizedServerPreferredAddress) { |
| 16317 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16318 | return; |
| 16319 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16320 | const QuicSocketAddress kNewSelfAddress = |
| 16321 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16322 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16323 | EXPECT_CALL(visitor_, |
| 16324 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16325 | .WillOnce(Invoke([&]() { |
| 16326 | connection_.ValidatePath( |
| 16327 | std::make_unique<TestQuicPathValidationContext>( |
| 16328 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16329 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16330 | &connection_), |
| 16331 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16332 | })); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16333 | QuicConfig config; |
fayang | 6aa0d53 | 2023-01-10 20:06:35 -0800 | [diff] [blame] | 16334 | config.SetClientConnectionOptions(QuicTagVector{kSPA2}); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16335 | ServerPreferredAddressInit(config); |
| 16336 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 16337 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
| 16338 | |
| 16339 | // Send data packet while path validation is pending. |
| 16340 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 16341 | // Verify the packet is sent on both paths. |
| 16342 | EXPECT_FALSE(writer_->stream_frames().empty()); |
| 16343 | EXPECT_FALSE(new_writer.stream_frames().empty()); |
| 16344 | |
| 16345 | // Verify packet duplication stops on handshake confirmed. |
| 16346 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16347 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16348 | connection_.OnHandshakeComplete(); |
| 16349 | SendPing(); |
| 16350 | EXPECT_FALSE(writer_->ping_frames().empty()); |
| 16351 | EXPECT_TRUE(new_writer.ping_frames().empty()); |
| 16352 | } |
| 16353 | |
| 16354 | TEST_P(QuicConnectionTest, OptimizedServerPreferredAddress2) { |
| 16355 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16356 | return; |
| 16357 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16358 | const QuicSocketAddress kNewSelfAddress = |
| 16359 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16360 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16361 | EXPECT_CALL(visitor_, |
| 16362 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16363 | .WillOnce(Invoke([&]() { |
| 16364 | connection_.ValidatePath( |
| 16365 | std::make_unique<TestQuicPathValidationContext>( |
| 16366 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16367 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16368 | &connection_), |
| 16369 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16370 | })); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16371 | QuicConfig config; |
fayang | 6aa0d53 | 2023-01-10 20:06:35 -0800 | [diff] [blame] | 16372 | config.SetClientConnectionOptions(QuicTagVector{kSPA2}); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16373 | ServerPreferredAddressInit(config); |
| 16374 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 16375 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
| 16376 | |
| 16377 | // Send data packet while path validation is pending. |
| 16378 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 16379 | // Verify the packet is sent on both paths. |
| 16380 | EXPECT_FALSE(writer_->stream_frames().empty()); |
| 16381 | EXPECT_FALSE(new_writer.stream_frames().empty()); |
| 16382 | |
| 16383 | // Simluate path validation times out. |
| 16384 | for (size_t i = 0; i < QuicPathValidator::kMaxRetryTimes + 1; ++i) { |
| 16385 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs)); |
| 16386 | static_cast<TestAlarmFactory::TestAlarm*>( |
| 16387 | QuicPathValidatorPeer::retry_timer( |
| 16388 | QuicConnectionPeer::path_validator(&connection_))) |
| 16389 | ->Fire(); |
| 16390 | } |
| 16391 | EXPECT_FALSE(connection_.HasPendingPathValidation()); |
| 16392 | // Verify packet duplication stops if there is no pending validation. |
| 16393 | SendPing(); |
| 16394 | EXPECT_FALSE(writer_->ping_frames().empty()); |
| 16395 | EXPECT_TRUE(new_writer.ping_frames().empty()); |
| 16396 | } |
| 16397 | |
| 16398 | TEST_P(QuicConnectionTest, MaxDuplicatedPacketsSentToServerPreferredAddress) { |
| 16399 | if (!connection_.version().HasIetfQuicFrames()) { |
| 16400 | return; |
| 16401 | } |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16402 | const QuicSocketAddress kNewSelfAddress = |
| 16403 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16404 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16405 | EXPECT_CALL(visitor_, |
| 16406 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16407 | .WillOnce(Invoke([&]() { |
| 16408 | connection_.ValidatePath( |
| 16409 | std::make_unique<TestQuicPathValidationContext>( |
| 16410 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16411 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16412 | &connection_), |
| 16413 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16414 | })); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16415 | QuicConfig config; |
fayang | 6aa0d53 | 2023-01-10 20:06:35 -0800 | [diff] [blame] | 16416 | config.SetClientConnectionOptions(QuicTagVector{kSPA2}); |
fayang | a0618a6 | 2022-12-28 19:31:24 -0800 | [diff] [blame] | 16417 | ServerPreferredAddressInit(config); |
| 16418 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 16419 | ASSERT_FALSE(new_writer.path_challenge_frames().empty()); |
| 16420 | |
| 16421 | // Send data packet while path validation is pending. |
| 16422 | size_t write_limit = writer_->packets_write_attempts(); |
| 16423 | size_t new_write_limit = new_writer.packets_write_attempts(); |
| 16424 | for (size_t i = 0; i < kMaxDuplicatedPacketsSentToServerPreferredAddress; |
| 16425 | ++i) { |
| 16426 | connection_.SendStreamDataWithString(3, "foo", i * 3, NO_FIN); |
| 16427 | // Verify the packet is sent on both paths. |
| 16428 | ASSERT_EQ(write_limit + 1, writer_->packets_write_attempts()); |
| 16429 | ASSERT_EQ(new_write_limit + 1, new_writer.packets_write_attempts()); |
| 16430 | ++write_limit; |
| 16431 | ++new_write_limit; |
| 16432 | EXPECT_FALSE(writer_->stream_frames().empty()); |
| 16433 | EXPECT_FALSE(new_writer.stream_frames().empty()); |
| 16434 | } |
| 16435 | |
| 16436 | // Verify packet duplication stops if duplication limit is hit. |
| 16437 | SendPing(); |
| 16438 | ASSERT_EQ(write_limit + 1, writer_->packets_write_attempts()); |
| 16439 | ASSERT_EQ(new_write_limit, new_writer.packets_write_attempts()); |
| 16440 | EXPECT_FALSE(writer_->ping_frames().empty()); |
| 16441 | EXPECT_TRUE(new_writer.ping_frames().empty()); |
| 16442 | } |
| 16443 | |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16444 | TEST_P(QuicConnectionTest, MultiPortCreationAfterServerMigration) { |
| 16445 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 16446 | return; |
| 16447 | } |
| 16448 | QuicConfig config; |
fayang | 6aa0d53 | 2023-01-10 20:06:35 -0800 | [diff] [blame] | 16449 | config.SetClientConnectionOptions(QuicTagVector{kMPQC}); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16450 | ServerPreferredAddressInit(config); |
| 16451 | if (!connection_.connection_migration_use_new_cid()) { |
| 16452 | return; |
| 16453 | } |
| 16454 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16455 | QuicConnectionId cid_for_preferred_address = TestConnectionId(17); |
| 16456 | const QuicSocketAddress kNewSelfAddress = |
| 16457 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 16458 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 16459 | EXPECT_CALL(visitor_, |
| 16460 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16461 | .WillOnce(Invoke([&]() { |
| 16462 | connection_.ValidatePath( |
| 16463 | std::make_unique<TestQuicPathValidationContext>( |
| 16464 | kNewSelfAddress, kServerPreferredAddress, &new_writer), |
| 16465 | std::make_unique<ServerPreferredAddressTestResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16466 | &connection_), |
| 16467 | PathValidationReason::kReasonUnknown); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16468 | })); |
| 16469 | // The connection should start probing the preferred address after handshake |
| 16470 | // confirmed. |
| 16471 | QuicPathFrameBuffer payload; |
| 16472 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 16473 | .Times(testing::AtLeast(1u)) |
| 16474 | .WillOnce(Invoke([&]() { |
| 16475 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 16476 | payload = new_writer.path_challenge_frames().front().data_buffer; |
| 16477 | EXPECT_EQ(kServerPreferredAddress, |
| 16478 | new_writer.last_write_peer_address()); |
| 16479 | })); |
| 16480 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16481 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16482 | connection_.OnHandshakeComplete(); |
| 16483 | EXPECT_TRUE(connection_.IsValidatingServerPreferredAddress()); |
| 16484 | |
| 16485 | // Receiving PATH_RESPONSE should cause the connection to migrate to the |
| 16486 | // preferred address. |
| 16487 | QuicFrames frames; |
| 16488 | frames.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
| 16489 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress, kPeerAddress, |
| 16490 | ENCRYPTION_FORWARD_SECURE); |
| 16491 | EXPECT_FALSE(connection_.IsValidatingServerPreferredAddress()); |
| 16492 | EXPECT_EQ(kServerPreferredAddress, connection_.effective_peer_address()); |
| 16493 | EXPECT_EQ(kNewSelfAddress, connection_.self_address()); |
| 16494 | EXPECT_EQ(connection_.connection_id(), cid_for_preferred_address); |
| 16495 | |
| 16496 | // As the default path changed, the server issued CID 1 should be retired. |
| 16497 | auto* retire_peer_issued_cid_alarm = |
| 16498 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16499 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16500 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 16501 | retire_peer_issued_cid_alarm->Fire(); |
| 16502 | |
| 16503 | const QuicSocketAddress kNewSelfAddress2(kNewSelfAddress.host(), |
| 16504 | kNewSelfAddress.port() + 1); |
| 16505 | EXPECT_NE(kNewSelfAddress2, kNewSelfAddress); |
| 16506 | TestPacketWriter new_writer2(version(), &clock_, Perspective::IS_CLIENT); |
| 16507 | QuicNewConnectionIdFrame frame; |
| 16508 | frame.connection_id = TestConnectionId(789); |
| 16509 | ASSERT_NE(frame.connection_id, connection_.connection_id()); |
| 16510 | frame.stateless_reset_token = |
| 16511 | QuicUtils::GenerateStatelessResetToken(frame.connection_id); |
| 16512 | frame.retire_prior_to = 0u; |
| 16513 | frame.sequence_number = 2u; |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 16514 | EXPECT_CALL(visitor_, CreateContextForMultiPortPath) |
QUICHE team | 11e17fe | 2023-05-12 08:21:20 -0700 | [diff] [blame] | 16515 | .WillOnce(testing::WithArgs<0>([&](auto&& observer) { |
| 16516 | observer->OnMultiPortPathContextAvailable( |
| 16517 | std::move(std::make_unique<TestQuicPathValidationContext>( |
| 16518 | kNewSelfAddress2, connection_.peer_address(), &new_writer2))); |
QUICHE team | 107316f | 2023-05-03 09:04:11 -0700 | [diff] [blame] | 16519 | })); |
danzh | 8fdee2e | 2023-01-05 15:33:02 -0800 | [diff] [blame] | 16520 | connection_.OnNewConnectionIdFrame(frame); |
| 16521 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 16522 | EXPECT_EQ(1u, new_writer.path_challenge_frames().size()); |
| 16523 | payload = new_writer.path_challenge_frames().front().data_buffer; |
| 16524 | EXPECT_EQ(kServerPreferredAddress, new_writer.last_write_peer_address()); |
| 16525 | EXPECT_EQ(kNewSelfAddress2.host(), new_writer.last_write_source_address()); |
| 16526 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 16527 | &connection_, kNewSelfAddress2, connection_.peer_address())); |
| 16528 | auto* alt_path = QuicConnectionPeer::GetAlternativePath(&connection_); |
| 16529 | EXPECT_FALSE(alt_path->validated); |
| 16530 | QuicFrames frames2; |
| 16531 | frames2.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
| 16532 | ProcessFramesPacketWithAddresses(frames2, kNewSelfAddress2, kPeerAddress, |
| 16533 | ENCRYPTION_FORWARD_SECURE); |
| 16534 | EXPECT_TRUE(alt_path->validated); |
| 16535 | } |
| 16536 | |
danzh | 72225ae | 2023-01-13 14:57:42 -0800 | [diff] [blame] | 16537 | // Tests that after half-way server migration, the client should be able to |
| 16538 | // respond to any reverse path validation from the original server address. |
| 16539 | TEST_P(QuicConnectionTest, ClientReceivePathChallengeAfterServerMigration) { |
| 16540 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 16541 | return; |
| 16542 | } |
| 16543 | QuicConfig config; |
| 16544 | ServerPreferredAddressInit(config); |
| 16545 | QuicConnectionId cid_for_preferred_address = TestConnectionId(17); |
| 16546 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16547 | EXPECT_CALL(visitor_, |
| 16548 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16549 | .WillOnce(Invoke([&]() { |
| 16550 | connection_.AddKnownServerAddress(kServerPreferredAddress); |
| 16551 | })); |
| 16552 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16553 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16554 | connection_.OnHandshakeComplete(); |
| 16555 | |
| 16556 | const QuicSocketAddress kNewSelfAddress = |
| 16557 | QuicSocketAddress(QuicIpAddress::Loopback6(), kTestPort + 1); |
| 16558 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 16559 | auto context = std::make_unique<TestQuicPathValidationContext>( |
| 16560 | kNewSelfAddress, kServerPreferredAddress, &new_writer); |
| 16561 | // Pretend that the validation already succeeded. And start to use the server |
| 16562 | // preferred address. |
| 16563 | connection_.OnServerPreferredAddressValidated(*context, false); |
| 16564 | EXPECT_EQ(kServerPreferredAddress, connection_.effective_peer_address()); |
| 16565 | EXPECT_EQ(kServerPreferredAddress, connection_.peer_address()); |
| 16566 | EXPECT_EQ(kNewSelfAddress, connection_.self_address()); |
| 16567 | EXPECT_EQ(connection_.connection_id(), cid_for_preferred_address); |
| 16568 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 16569 | send_algorithm_); |
| 16570 | // Switch to use a mock send algorithm. |
| 16571 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 16572 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 16573 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 16574 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 16575 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 16576 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 16577 | .Times(AnyNumber()) |
| 16578 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 16579 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 16580 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 16581 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 16582 | connection_.SetSendAlgorithm(send_algorithm_); |
| 16583 | |
| 16584 | // As the default path changed, the server issued CID 123 should be retired. |
| 16585 | QuicConnectionPeer::RetirePeerIssuedConnectionIdsNoLongerOnPath(&connection_); |
| 16586 | auto* retire_peer_issued_cid_alarm = |
| 16587 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16588 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16589 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 16590 | retire_peer_issued_cid_alarm->Fire(); |
| 16591 | |
| 16592 | // Receive PATH_CHALLENGE from the original server |
| 16593 | // address. The client connection responds it on the default path. |
| 16594 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 16595 | QuicFrames frames1; |
| 16596 | frames1.push_back( |
| 16597 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
| 16598 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 16599 | .Times(AtLeast(1)) |
| 16600 | .WillOnce(Invoke([&]() { |
| 16601 | ASSERT_FALSE(new_writer.path_response_frames().empty()); |
| 16602 | EXPECT_EQ( |
| 16603 | 0, memcmp(&path_challenge_payload, |
| 16604 | &(new_writer.path_response_frames().front().data_buffer), |
| 16605 | sizeof(path_challenge_payload))); |
| 16606 | EXPECT_EQ(kServerPreferredAddress, |
| 16607 | new_writer.last_write_peer_address()); |
| 16608 | EXPECT_EQ(kNewSelfAddress.host(), |
| 16609 | new_writer.last_write_source_address()); |
| 16610 | })); |
| 16611 | ProcessFramesPacketWithAddresses(frames1, kNewSelfAddress, kPeerAddress, |
| 16612 | ENCRYPTION_FORWARD_SECURE); |
| 16613 | } |
| 16614 | |
| 16615 | // Tests that after half-way server migration, the client should be able to |
| 16616 | // probe with a different socket and respond to reverse path validation. |
| 16617 | TEST_P(QuicConnectionTest, ClientProbesAfterServerMigration) { |
| 16618 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 16619 | return; |
| 16620 | } |
| 16621 | QuicConfig config; |
| 16622 | ServerPreferredAddressInit(config); |
| 16623 | QuicConnectionId cid_for_preferred_address = TestConnectionId(17); |
| 16624 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16625 | |
| 16626 | // The connection should start probing the preferred address after handshake |
| 16627 | // confirmed. |
| 16628 | EXPECT_CALL(visitor_, |
| 16629 | OnServerPreferredAddressAvailable(kServerPreferredAddress)) |
| 16630 | .WillOnce(Invoke([&]() { |
| 16631 | connection_.AddKnownServerAddress(kServerPreferredAddress); |
| 16632 | })); |
| 16633 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 16634 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
| 16635 | connection_.OnHandshakeComplete(); |
| 16636 | |
| 16637 | const QuicSocketAddress kNewSelfAddress = |
| 16638 | QuicSocketAddress(QuicIpAddress::Loopback6(), kTestPort + 1); |
| 16639 | TestPacketWriter new_writer(version(), &clock_, Perspective::IS_CLIENT); |
| 16640 | auto context = std::make_unique<TestQuicPathValidationContext>( |
| 16641 | kNewSelfAddress, kServerPreferredAddress, &new_writer); |
| 16642 | // Pretend that the validation already succeeded. |
| 16643 | connection_.OnServerPreferredAddressValidated(*context, false); |
| 16644 | EXPECT_EQ(kServerPreferredAddress, connection_.effective_peer_address()); |
| 16645 | EXPECT_EQ(kServerPreferredAddress, connection_.peer_address()); |
| 16646 | EXPECT_EQ(kNewSelfAddress, connection_.self_address()); |
| 16647 | EXPECT_EQ(connection_.connection_id(), cid_for_preferred_address); |
| 16648 | EXPECT_NE(connection_.sent_packet_manager().GetSendAlgorithm(), |
| 16649 | send_algorithm_); |
| 16650 | // Switch to use a mock send algorithm. |
| 16651 | send_algorithm_ = new StrictMock<MockSendAlgorithm>(); |
| 16652 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 16653 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 16654 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 16655 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 16656 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 16657 | .Times(AnyNumber()) |
| 16658 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 16659 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 16660 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 16661 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)).Times(AnyNumber()); |
| 16662 | connection_.SetSendAlgorithm(send_algorithm_); |
| 16663 | |
| 16664 | // Receiving data from the original server address should not change the peer |
| 16665 | // address. |
| 16666 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 16667 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kNewSelfAddress, |
| 16668 | kPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 16669 | EXPECT_EQ(kServerPreferredAddress, connection_.effective_peer_address()); |
| 16670 | EXPECT_EQ(kServerPreferredAddress, connection_.peer_address()); |
| 16671 | |
| 16672 | // As the default path changed, the server issued CID 123 should be retired. |
| 16673 | auto* retire_peer_issued_cid_alarm = |
| 16674 | connection_.GetRetirePeerIssuedConnectionIdAlarm(); |
| 16675 | ASSERT_TRUE(retire_peer_issued_cid_alarm->IsSet()); |
| 16676 | EXPECT_CALL(visitor_, SendRetireConnectionId(/*sequence_number=*/0u)); |
| 16677 | retire_peer_issued_cid_alarm->Fire(); |
| 16678 | |
| 16679 | // Receiving a new CID from the server. |
| 16680 | QuicNewConnectionIdFrame new_cid_frame1; |
| 16681 | new_cid_frame1.connection_id = TestConnectionId(456); |
| 16682 | ASSERT_NE(new_cid_frame1.connection_id, connection_.connection_id()); |
| 16683 | new_cid_frame1.stateless_reset_token = |
| 16684 | QuicUtils::GenerateStatelessResetToken(new_cid_frame1.connection_id); |
| 16685 | new_cid_frame1.retire_prior_to = 0u; |
| 16686 | new_cid_frame1.sequence_number = 2u; |
| 16687 | connection_.OnNewConnectionIdFrame(new_cid_frame1); |
| 16688 | |
| 16689 | // Probe from a new socket. |
| 16690 | const QuicSocketAddress kNewSelfAddress2 = |
| 16691 | QuicSocketAddress(QuicIpAddress::Loopback4(), kTestPort + 2); |
| 16692 | TestPacketWriter new_writer2(version(), &clock_, Perspective::IS_CLIENT); |
| 16693 | bool success; |
| 16694 | QuicPathFrameBuffer payload; |
| 16695 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 16696 | .Times(testing::AtLeast(1u)) |
| 16697 | .WillOnce(Invoke([&]() { |
| 16698 | EXPECT_EQ(1u, new_writer2.path_challenge_frames().size()); |
| 16699 | payload = new_writer2.path_challenge_frames().front().data_buffer; |
| 16700 | EXPECT_EQ(kServerPreferredAddress, |
| 16701 | new_writer2.last_write_peer_address()); |
| 16702 | EXPECT_EQ(kNewSelfAddress2.host(), |
| 16703 | new_writer2.last_write_source_address()); |
| 16704 | })); |
| 16705 | connection_.ValidatePath( |
| 16706 | std::make_unique<TestQuicPathValidationContext>( |
| 16707 | kNewSelfAddress2, connection_.peer_address(), &new_writer2), |
| 16708 | std::make_unique<TestValidationResultDelegate>( |
renjietang | e499db4 | 2023-01-17 15:42:33 -0800 | [diff] [blame] | 16709 | &connection_, kNewSelfAddress2, connection_.peer_address(), &success), |
| 16710 | PathValidationReason::kServerPreferredAddressMigration); |
danzh | 72225ae | 2023-01-13 14:57:42 -0800 | [diff] [blame] | 16711 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath( |
| 16712 | &connection_, kNewSelfAddress2, kServerPreferredAddress)); |
| 16713 | |
| 16714 | // Our server implementation will send PATH_CHALLENGE from the original server |
| 16715 | // address. The client connection send PATH_RESPONSE to the default peer |
| 16716 | // address. |
| 16717 | QuicPathFrameBuffer path_challenge_payload{0, 1, 2, 3, 4, 5, 6, 7}; |
| 16718 | QuicFrames frames; |
| 16719 | frames.push_back( |
| 16720 | QuicFrame(QuicPathChallengeFrame(0, path_challenge_payload))); |
| 16721 | frames.push_back(QuicFrame(QuicPathResponseFrame(99, payload))); |
| 16722 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 16723 | .Times(AtLeast(1)) |
| 16724 | .WillOnce(Invoke([&]() { |
| 16725 | EXPECT_FALSE(new_writer2.path_response_frames().empty()); |
| 16726 | EXPECT_EQ( |
| 16727 | 0, memcmp(&path_challenge_payload, |
| 16728 | &(new_writer2.path_response_frames().front().data_buffer), |
| 16729 | sizeof(path_challenge_payload))); |
| 16730 | EXPECT_EQ(kServerPreferredAddress, |
| 16731 | new_writer2.last_write_peer_address()); |
| 16732 | EXPECT_EQ(kNewSelfAddress2.host(), |
| 16733 | new_writer2.last_write_source_address()); |
| 16734 | })); |
| 16735 | ProcessFramesPacketWithAddresses(frames, kNewSelfAddress2, kPeerAddress, |
| 16736 | ENCRYPTION_FORWARD_SECURE); |
| 16737 | EXPECT_TRUE(success); |
| 16738 | } |
| 16739 | |
martinduke | fcfa32a | 2023-01-12 10:04:44 -0800 | [diff] [blame] | 16740 | TEST_P(QuicConnectionTest, EcnMarksCorrectlyRecorded) { |
| 16741 | set_perspective(Perspective::IS_SERVER); |
martinduke | fcfa32a | 2023-01-12 10:04:44 -0800 | [diff] [blame] | 16742 | QuicFrames frames; |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16743 | frames.push_back(QuicFrame(QuicPingFrame())); |
| 16744 | frames.push_back(QuicFrame(QuicPaddingFrame(7))); |
| 16745 | QuicAckFrame ack_frame = |
| 16746 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16747 | ? connection_.received_packet_manager().GetAckFrame(APPLICATION_DATA) |
| 16748 | : connection_.received_packet_manager().ack_frame(); |
| 16749 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16750 | |
| 16751 | ProcessFramesPacketAtLevelWithEcn(1, frames, ENCRYPTION_FORWARD_SECURE, |
| 16752 | ECN_ECT0); |
| 16753 | ack_frame = |
| 16754 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16755 | ? connection_.received_packet_manager().GetAckFrame(APPLICATION_DATA) |
| 16756 | : connection_.received_packet_manager().ack_frame(); |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16757 | // Send two PINGs so that the ACK goes too. The second packet should not |
| 16758 | // include an ACK, which checks that the packet state is cleared properly. |
| 16759 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16760 | if (connection_.version().HasIetfQuicFrames()) { |
| 16761 | QuicConnectionPeer::SendPing(&connection_); |
| 16762 | QuicConnectionPeer::SendPing(&connection_); |
| 16763 | } |
| 16764 | QuicConnectionStats stats = connection_.GetStats(); |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16765 | if (GetQuicRestartFlag(quic_receive_ecn)) { |
| 16766 | ASSERT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16767 | EXPECT_EQ(ack_frame.ecn_counters->ect0, 1); |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16768 | EXPECT_EQ(stats.num_ack_frames_sent_with_ecn, |
| 16769 | connection_.version().HasIetfQuicFrames() ? 1 : 0); |
martinduke | fcfa32a | 2023-01-12 10:04:44 -0800 | [diff] [blame] | 16770 | } else { |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16771 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16772 | EXPECT_EQ(stats.num_ack_frames_sent_with_ecn, 0); |
martinduke | fcfa32a | 2023-01-12 10:04:44 -0800 | [diff] [blame] | 16773 | } |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16774 | EXPECT_EQ(stats.num_ecn_marks_received.ect0, 1); |
| 16775 | EXPECT_EQ(stats.num_ecn_marks_received.ect1, 0); |
| 16776 | EXPECT_EQ(stats.num_ecn_marks_received.ce, 0); |
martinduke | fcfa32a | 2023-01-12 10:04:44 -0800 | [diff] [blame] | 16777 | } |
| 16778 | |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16779 | TEST_P(QuicConnectionTest, EcnMarksCoalescedPacket) { |
| 16780 | if (!connection_.version().CanSendCoalescedPackets() || |
| 16781 | !GetQuicRestartFlag(quic_receive_ecn)) { |
| 16782 | return; |
| 16783 | } |
| 16784 | QuicCryptoFrame crypto_frame1{ENCRYPTION_HANDSHAKE, 0, "foo"}; |
| 16785 | QuicFrames frames1; |
| 16786 | frames1.push_back(QuicFrame(&crypto_frame1)); |
| 16787 | QuicFrames frames2; |
| 16788 | QuicCryptoFrame crypto_frame2{ENCRYPTION_FORWARD_SECURE, 0, "bar"}; |
| 16789 | frames2.push_back(QuicFrame(&crypto_frame2)); |
| 16790 | std::vector<PacketInfo> packets = {{2, frames1, ENCRYPTION_HANDSHAKE}, |
| 16791 | {3, frames2, ENCRYPTION_FORWARD_SECURE}}; |
| 16792 | QuicAckFrame ack_frame = |
| 16793 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16794 | ? connection_.received_packet_manager().GetAckFrame(APPLICATION_DATA) |
| 16795 | : connection_.received_packet_manager().ack_frame(); |
| 16796 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16797 | ack_frame = |
| 16798 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16799 | ? connection_.received_packet_manager().GetAckFrame(HANDSHAKE_DATA) |
| 16800 | : connection_.received_packet_manager().ack_frame(); |
| 16801 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16802 | // Deliver packets. |
| 16803 | connection_.SetEncrypter( |
| 16804 | ENCRYPTION_HANDSHAKE, |
| 16805 | std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); |
| 16806 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(2); |
| 16807 | ProcessCoalescedPacket(packets, ECN_ECT0); |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16808 | // Send two PINGs so that the ACKs go too. |
| 16809 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16810 | if (connection_.version().HasIetfQuicFrames()) { |
| 16811 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 16812 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 16813 | QuicConnectionPeer::SendPing(&connection_); |
| 16814 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 16815 | QuicConnectionPeer::SendPing(&connection_); |
| 16816 | } |
| 16817 | QuicConnectionStats stats = connection_.GetStats(); |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16818 | ack_frame = |
| 16819 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16820 | ? connection_.received_packet_manager().GetAckFrame(HANDSHAKE_DATA) |
| 16821 | : connection_.received_packet_manager().ack_frame(); |
| 16822 | ASSERT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16823 | EXPECT_EQ(ack_frame.ecn_counters->ect0, |
| 16824 | connection_.SupportsMultiplePacketNumberSpaces() ? 1 : 2); |
| 16825 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 16826 | ack_frame = connection_.SupportsMultiplePacketNumberSpaces() |
| 16827 | ? connection_.received_packet_manager().GetAckFrame( |
| 16828 | APPLICATION_DATA) |
| 16829 | : connection_.received_packet_manager().ack_frame(); |
| 16830 | EXPECT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16831 | EXPECT_EQ(ack_frame.ecn_counters->ect0, 1); |
| 16832 | } |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16833 | if (GetQuicRestartFlag(quic_receive_ecn)) { |
| 16834 | EXPECT_EQ(stats.num_ecn_marks_received.ect0, 2); |
| 16835 | EXPECT_EQ(stats.num_ack_frames_sent_with_ecn, |
| 16836 | connection_.version().HasIetfQuicFrames() ? 2 : 0); |
| 16837 | } else { |
| 16838 | EXPECT_EQ(stats.num_ecn_marks_received.ect0, 0); |
| 16839 | EXPECT_EQ(stats.num_ack_frames_sent_with_ecn, 0); |
| 16840 | } |
| 16841 | EXPECT_EQ(stats.num_ecn_marks_received.ect1, 0); |
| 16842 | EXPECT_EQ(stats.num_ecn_marks_received.ce, 0); |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16843 | } |
| 16844 | |
| 16845 | TEST_P(QuicConnectionTest, EcnMarksUndecryptableCoalescedPacket) { |
| 16846 | if (!connection_.version().CanSendCoalescedPackets() || |
| 16847 | !GetQuicRestartFlag(quic_receive_ecn)) { |
| 16848 | return; |
| 16849 | } |
| 16850 | // SetFromConfig is always called after construction from InitializeSession. |
| 16851 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 16852 | QuicConfig config; |
| 16853 | config.set_max_undecryptable_packets(100); |
| 16854 | connection_.SetFromConfig(config); |
| 16855 | QuicCryptoFrame crypto_frame1{ENCRYPTION_HANDSHAKE, 0, "foo"}; |
| 16856 | QuicFrames frames1; |
| 16857 | frames1.push_back(QuicFrame(&crypto_frame1)); |
| 16858 | QuicFrames frames2; |
| 16859 | QuicCryptoFrame crypto_frame2{ENCRYPTION_FORWARD_SECURE, 0, "bar"}; |
| 16860 | frames2.push_back(QuicFrame(&crypto_frame2)); |
| 16861 | std::vector<PacketInfo> packets = {{2, frames1, ENCRYPTION_HANDSHAKE}, |
| 16862 | {3, frames2, ENCRYPTION_FORWARD_SECURE}}; |
| 16863 | char coalesced_buffer[kMaxOutgoingPacketSize]; |
| 16864 | size_t coalesced_size = 0; |
| 16865 | for (const auto& packet : packets) { |
| 16866 | QuicPacketHeader header = |
| 16867 | ConstructPacketHeader(packet.packet_number, packet.level); |
| 16868 | // Set the correct encryption level and encrypter on peer_creator and |
| 16869 | // peer_framer, respectively. |
| 16870 | peer_creator_.set_encryption_level(packet.level); |
| 16871 | peer_framer_.SetEncrypter(packet.level, |
| 16872 | std::make_unique<TaggingEncrypter>(packet.level)); |
| 16873 | // Set the corresponding decrypter. |
| 16874 | if (packet.level == ENCRYPTION_HANDSHAKE) { |
| 16875 | connection_.SetEncrypter( |
| 16876 | packet.level, std::make_unique<TaggingEncrypter>(packet.level)); |
| 16877 | connection_.SetDefaultEncryptionLevel(packet.level); |
| 16878 | SetDecrypter(packet.level, |
| 16879 | std::make_unique<StrictTaggingDecrypter>(packet.level)); |
| 16880 | } |
| 16881 | // Forward Secure packet is undecryptable. |
| 16882 | std::unique_ptr<QuicPacket> constructed_packet( |
| 16883 | ConstructPacket(header, packet.frames)); |
| 16884 | |
| 16885 | char buffer[kMaxOutgoingPacketSize]; |
| 16886 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 16887 | packet.level, QuicPacketNumber(packet.packet_number), |
| 16888 | *constructed_packet, buffer, kMaxOutgoingPacketSize); |
| 16889 | QUICHE_DCHECK_LE(coalesced_size + encrypted_length, kMaxOutgoingPacketSize); |
| 16890 | memcpy(coalesced_buffer + coalesced_size, buffer, encrypted_length); |
| 16891 | coalesced_size += encrypted_length; |
| 16892 | } |
| 16893 | QuicAckFrame ack_frame = |
| 16894 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16895 | ? connection_.received_packet_manager().GetAckFrame(APPLICATION_DATA) |
| 16896 | : connection_.received_packet_manager().ack_frame(); |
| 16897 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16898 | ack_frame = |
| 16899 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16900 | ? connection_.received_packet_manager().GetAckFrame(HANDSHAKE_DATA) |
| 16901 | : connection_.received_packet_manager().ack_frame(); |
| 16902 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16903 | // Deliver packets, but first remove the Forward Secure decrypter so that |
| 16904 | // packet has to be buffered. |
| 16905 | connection_.RemoveDecrypter(ENCRYPTION_FORWARD_SECURE); |
| 16906 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 16907 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 16908 | connection_.ProcessUdpPacket( |
| 16909 | kSelfAddress, kPeerAddress, |
| 16910 | QuicReceivedPacket(coalesced_buffer, coalesced_size, clock_.Now(), false, |
| 16911 | 0, true, nullptr, 0, true, ECN_ECT0)); |
| 16912 | if (connection_.GetSendAlarm()->IsSet()) { |
| 16913 | connection_.GetSendAlarm()->Fire(); |
| 16914 | } |
| 16915 | ack_frame = |
| 16916 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16917 | ? connection_.received_packet_manager().GetAckFrame(HANDSHAKE_DATA) |
| 16918 | : connection_.received_packet_manager().ack_frame(); |
| 16919 | ASSERT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16920 | EXPECT_EQ(ack_frame.ecn_counters->ect0, 1); |
| 16921 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 16922 | ack_frame = connection_.SupportsMultiplePacketNumberSpaces() |
| 16923 | ? connection_.received_packet_manager().GetAckFrame( |
| 16924 | APPLICATION_DATA) |
| 16925 | : connection_.received_packet_manager().ack_frame(); |
| 16926 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16927 | } |
| 16928 | // Send PING packet with ECN_CE, which will change the ECN codepoint in |
| 16929 | // last_received_packet_info_. |
| 16930 | ProcessFramePacketAtLevelWithEcn(4, QuicFrame(QuicPingFrame()), |
| 16931 | ENCRYPTION_HANDSHAKE, ECN_CE); |
| 16932 | ack_frame = |
| 16933 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16934 | ? connection_.received_packet_manager().GetAckFrame(HANDSHAKE_DATA) |
| 16935 | : connection_.received_packet_manager().ack_frame(); |
| 16936 | ASSERT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16937 | EXPECT_EQ(ack_frame.ecn_counters->ect0, 1); |
| 16938 | EXPECT_EQ(ack_frame.ecn_counters->ce, 1); |
| 16939 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 16940 | ack_frame = connection_.SupportsMultiplePacketNumberSpaces() |
| 16941 | ? connection_.received_packet_manager().GetAckFrame( |
| 16942 | APPLICATION_DATA) |
| 16943 | : connection_.received_packet_manager().ack_frame(); |
| 16944 | EXPECT_FALSE(ack_frame.ecn_counters.has_value()); |
| 16945 | } |
| 16946 | // Install decrypter for ENCRYPTION_FORWARD_SECURE. Make sure the original |
| 16947 | // ECN codepoint is incremented. |
| 16948 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 16949 | SetDecrypter( |
| 16950 | ENCRYPTION_FORWARD_SECURE, |
| 16951 | std::make_unique<StrictTaggingDecrypter>(ENCRYPTION_FORWARD_SECURE)); |
| 16952 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 16953 | ack_frame = |
| 16954 | connection_.SupportsMultiplePacketNumberSpaces() |
| 16955 | ? connection_.received_packet_manager().GetAckFrame(APPLICATION_DATA) |
| 16956 | : connection_.received_packet_manager().ack_frame(); |
| 16957 | ASSERT_TRUE(ack_frame.ecn_counters.has_value()); |
| 16958 | // Should be recorded as ECT(0), not CE. |
| 16959 | EXPECT_EQ(ack_frame.ecn_counters->ect0, |
| 16960 | connection_.SupportsMultiplePacketNumberSpaces() ? 1 : 2); |
martinduke | 01ee322 | 2023-03-08 17:08:09 -0800 | [diff] [blame] | 16961 | QuicConnectionStats stats = connection_.GetStats(); |
| 16962 | EXPECT_EQ(stats.num_ecn_marks_received.ect0, |
| 16963 | GetQuicRestartFlag(quic_receive_ecn) ? 2 : 0); |
| 16964 | EXPECT_EQ(stats.num_ecn_marks_received.ect1, 0); |
| 16965 | EXPECT_EQ(stats.num_ecn_marks_received.ce, |
| 16966 | GetQuicRestartFlag(quic_receive_ecn) ? 1 : 0); |
martinduke | f33b1da | 2023-01-12 14:14:29 -0800 | [diff] [blame] | 16967 | } |
| 16968 | |
martinduke | 0bfe732 | 2023-01-18 07:36:34 -0800 | [diff] [blame] | 16969 | TEST_P(QuicConnectionTest, ReceivedPacketInfoDefaults) { |
martinduke | 74528e5 | 2023-01-13 12:24:21 -0800 | [diff] [blame] | 16970 | EXPECT_TRUE(QuicConnectionPeer::TestLastReceivedPacketInfoDefaults()); |
| 16971 | } |
| 16972 | |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 16973 | TEST_P(QuicConnectionTest, DetectMigrationToPreferredAddress) { |
| 16974 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 16975 | return; |
| 16976 | } |
| 16977 | ServerHandlePreferredAddressInit(); |
| 16978 | |
| 16979 | // Issue a new server CID associated with the preferred address. |
| 16980 | QuicConnectionId server_issued_cid_for_preferred_address = |
| 16981 | TestConnectionId(17); |
| 16982 | EXPECT_CALL(connection_id_generator_, |
| 16983 | GenerateNextConnectionId(connection_id_)) |
| 16984 | .WillOnce(Return(server_issued_cid_for_preferred_address)); |
| 16985 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)).WillOnce(Return(true)); |
| 16986 | absl::optional<QuicNewConnectionIdFrame> frame = |
| 16987 | connection_.MaybeIssueNewConnectionIdForPreferredAddress(); |
| 16988 | ASSERT_TRUE(frame.has_value()); |
| 16989 | |
| 16990 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 16991 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), |
| 16992 | connection_.client_connection_id()); |
| 16993 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), connection_id_); |
| 16994 | |
| 16995 | // Process a packet received at the preferred Address. |
| 16996 | peer_creator_.SetServerConnectionId(server_issued_cid_for_preferred_address); |
| 16997 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 16998 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kServerPreferredAddress, |
| 16999 | kPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 17000 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 17001 | // The server migrates half-way with the default path unchanged, and |
| 17002 | // continuing with the client issued CID 1. |
| 17003 | EXPECT_EQ(kSelfAddress.host(), writer_->last_write_source_address()); |
| 17004 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 17005 | |
| 17006 | // The peer retires CID 123. |
| 17007 | QuicRetireConnectionIdFrame retire_cid_frame; |
| 17008 | retire_cid_frame.sequence_number = 0u; |
| 17009 | EXPECT_CALL(connection_id_generator_, |
| 17010 | GenerateNextConnectionId(server_issued_cid_for_preferred_address)) |
| 17011 | .WillOnce(Return(TestConnectionId(456))); |
| 17012 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)).WillOnce(Return(true)); |
| 17013 | EXPECT_CALL(visitor_, SendNewConnectionId(_)); |
| 17014 | EXPECT_TRUE(connection_.OnRetireConnectionIdFrame(retire_cid_frame)); |
| 17015 | |
| 17016 | // Process another packet received at Preferred Address. |
| 17017 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 17018 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kServerPreferredAddress, |
| 17019 | kPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 17020 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 17021 | EXPECT_EQ(kSelfAddress.host(), writer_->last_write_source_address()); |
| 17022 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 17023 | } |
| 17024 | |
| 17025 | TEST_P(QuicConnectionTest, |
| 17026 | DetectSimutanuousServerAndClientAddressChangeWithProbe) { |
| 17027 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 17028 | return; |
| 17029 | } |
| 17030 | ServerHandlePreferredAddressInit(); |
| 17031 | |
| 17032 | // Issue a new server CID associated with the preferred address. |
| 17033 | QuicConnectionId server_issued_cid_for_preferred_address = |
| 17034 | TestConnectionId(17); |
| 17035 | EXPECT_CALL(connection_id_generator_, |
| 17036 | GenerateNextConnectionId(connection_id_)) |
| 17037 | .WillOnce(Return(server_issued_cid_for_preferred_address)); |
| 17038 | EXPECT_CALL(visitor_, MaybeReserveConnectionId(_)).WillOnce(Return(true)); |
| 17039 | absl::optional<QuicNewConnectionIdFrame> frame = |
| 17040 | connection_.MaybeIssueNewConnectionIdForPreferredAddress(); |
| 17041 | ASSERT_TRUE(frame.has_value()); |
| 17042 | |
| 17043 | auto* packet_creator = QuicConnectionPeer::GetPacketCreator(&connection_); |
| 17044 | ASSERT_EQ(packet_creator->GetSourceConnectionId(), connection_id_); |
| 17045 | ASSERT_EQ(packet_creator->GetDestinationConnectionId(), |
| 17046 | connection_.client_connection_id()); |
| 17047 | |
| 17048 | // Receiving a probing packet from a new client address to the preferred |
| 17049 | // address. |
| 17050 | peer_creator_.SetServerConnectionId(server_issued_cid_for_preferred_address); |
| 17051 | const QuicSocketAddress kNewPeerAddress(QuicIpAddress::Loopback4(), |
| 17052 | /*port=*/34567); |
| 17053 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
| 17054 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 17055 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 17056 | probing_packet->encrypted_length), |
| 17057 | clock_.Now())); |
| 17058 | uint64_t num_probing_received = |
| 17059 | connection_.GetStats().num_connectivity_probing_received; |
| 17060 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 17061 | .Times(AtLeast(1u)) |
| 17062 | .WillOnce(Invoke([&]() { |
| 17063 | EXPECT_EQ(1u, writer_->path_response_frames().size()); |
| 17064 | EXPECT_EQ(1u, writer_->path_challenge_frames().size()); |
fayang | 1f578d3 | 2023-03-20 11:36:15 -0700 | [diff] [blame] | 17065 | // The responses should be sent from preferred address given server |
| 17066 | // has not received packet on original address from the new client |
| 17067 | // address. |
| 17068 | EXPECT_EQ(kServerPreferredAddress.host(), |
| 17069 | writer_->last_write_source_address()); |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 17070 | EXPECT_EQ(kNewPeerAddress, writer_->last_write_peer_address()); |
| 17071 | })); |
| 17072 | ProcessReceivedPacket(kServerPreferredAddress, kNewPeerAddress, *received); |
| 17073 | EXPECT_EQ(num_probing_received + 1, |
| 17074 | connection_.GetStats().num_connectivity_probing_received); |
| 17075 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath(&connection_, kSelfAddress, |
| 17076 | kNewPeerAddress)); |
| 17077 | EXPECT_LT(0u, QuicConnectionPeer::BytesSentOnAlternativePath(&connection_)); |
| 17078 | EXPECT_EQ(received->length(), |
| 17079 | QuicConnectionPeer::BytesReceivedOnAlternativePath(&connection_)); |
| 17080 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 17081 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 17082 | |
| 17083 | // Process a data packet received at the preferred Address from the new client |
| 17084 | // address. |
| 17085 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)); |
| 17086 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 17087 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kServerPreferredAddress, |
| 17088 | kNewPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 17089 | // The server migrates half-way with the new peer address but the same default |
| 17090 | // self address. |
| 17091 | EXPECT_EQ(kSelfAddress.host(), writer_->last_write_source_address()); |
| 17092 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 17093 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 17094 | EXPECT_TRUE(connection_.HasPendingPathValidation()); |
| 17095 | EXPECT_FALSE(QuicConnectionPeer::GetDefaultPath(&connection_)->validated); |
| 17096 | EXPECT_TRUE(QuicConnectionPeer::IsAlternativePath(&connection_, kSelfAddress, |
| 17097 | kPeerAddress)); |
| 17098 | EXPECT_EQ(packet_creator->GetSourceConnectionId(), |
| 17099 | server_issued_cid_for_preferred_address); |
| 17100 | |
| 17101 | // Process another packet received at the preferred Address. |
| 17102 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 17103 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kServerPreferredAddress, |
| 17104 | kNewPeerAddress, ENCRYPTION_FORWARD_SECURE); |
| 17105 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
fayang | 1f578d3 | 2023-03-20 11:36:15 -0700 | [diff] [blame] | 17106 | EXPECT_EQ(kServerPreferredAddress.host(), |
| 17107 | writer_->last_write_source_address()); |
danzh | b159ab0 | 2023-01-30 10:58:46 -0800 | [diff] [blame] | 17108 | EXPECT_EQ(kSelfAddress, connection_.self_address()); |
| 17109 | } |
| 17110 | |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17111 | TEST_P(QuicConnectionTest, EcnCodepointsRejected) { |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17112 | SetQuicReloadableFlag(quic_send_ect1, true); |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17113 | TestPerPacketOptions per_packet_options; |
| 17114 | connection_.set_per_packet_options(&per_packet_options); |
| 17115 | for (QuicEcnCodepoint ecn : {ECN_NOT_ECT, ECN_ECT0, ECN_ECT1, ECN_CE}) { |
| 17116 | per_packet_options.ecn_codepoint = ecn; |
| 17117 | if (ecn == ECN_ECT0) { |
| 17118 | EXPECT_CALL(*send_algorithm_, SupportsECT0()).WillOnce(Return(false)); |
| 17119 | } else if (ecn == ECN_ECT1) { |
| 17120 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillOnce(Return(false)); |
| 17121 | } |
| 17122 | EXPECT_CALL(connection_, OnSerializedPacket(_)); |
| 17123 | SendPing(); |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17124 | EXPECT_EQ(per_packet_options.ecn_codepoint, ecn); |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17125 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_NOT_ECT); |
| 17126 | } |
| 17127 | } |
| 17128 | |
| 17129 | TEST_P(QuicConnectionTest, EcnCodepointsAccepted) { |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17130 | SetQuicReloadableFlag(quic_send_ect1, true); |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17131 | TestPerPacketOptions per_packet_options; |
| 17132 | connection_.set_per_packet_options(&per_packet_options); |
| 17133 | for (QuicEcnCodepoint ecn : {ECN_NOT_ECT, ECN_ECT0, ECN_ECT1, ECN_CE}) { |
| 17134 | per_packet_options.ecn_codepoint = ecn; |
| 17135 | if (ecn == ECN_ECT0) { |
| 17136 | EXPECT_CALL(*send_algorithm_, SupportsECT0()).WillOnce(Return(true)); |
| 17137 | } else if (ecn == ECN_ECT1) { |
| 17138 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillOnce(Return(true)); |
| 17139 | } |
| 17140 | EXPECT_CALL(connection_, OnSerializedPacket(_)); |
| 17141 | SendPing(); |
| 17142 | QuicEcnCodepoint expected_codepoint = ecn; |
| 17143 | if (ecn == ECN_CE) { |
| 17144 | expected_codepoint = ECN_NOT_ECT; |
| 17145 | } |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17146 | EXPECT_EQ(per_packet_options.ecn_codepoint, ecn); |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17147 | EXPECT_EQ(writer_->last_ecn_sent(), expected_codepoint); |
| 17148 | } |
| 17149 | } |
| 17150 | |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17151 | TEST_P(QuicConnectionTest, EcnCodepointsRejectedIfFlagIsFalse) { |
| 17152 | SetQuicReloadableFlag(quic_send_ect1, false); |
| 17153 | TestPerPacketOptions per_packet_options; |
| 17154 | connection_.set_per_packet_options(&per_packet_options); |
| 17155 | for (QuicEcnCodepoint ecn : {ECN_NOT_ECT, ECN_ECT0, ECN_ECT1, ECN_CE}) { |
| 17156 | per_packet_options.ecn_codepoint = ecn; |
| 17157 | EXPECT_CALL(connection_, OnSerializedPacket(_)); |
| 17158 | SendPing(); |
| 17159 | EXPECT_EQ(per_packet_options.ecn_codepoint, ECN_NOT_ECT); |
| 17160 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_NOT_ECT); |
| 17161 | } |
| 17162 | } |
| 17163 | |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17164 | TEST_P(QuicConnectionTest, EcnValidationDisabled) { |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17165 | SetQuicReloadableFlag(quic_send_ect1, true); |
martinduke | 007b2ca | 2023-04-10 10:43:38 -0700 | [diff] [blame] | 17166 | TestPerPacketOptions per_packet_options; |
| 17167 | connection_.set_per_packet_options(&per_packet_options); |
| 17168 | QuicConnectionPeer::DisableEcnCodepointValidation(&connection_); |
| 17169 | for (QuicEcnCodepoint ecn : {ECN_NOT_ECT, ECN_ECT0, ECN_ECT1, ECN_CE}) { |
| 17170 | per_packet_options.ecn_codepoint = ecn; |
| 17171 | EXPECT_CALL(connection_, OnSerializedPacket(_)); |
| 17172 | SendPing(); |
| 17173 | EXPECT_EQ(per_packet_options.ecn_codepoint, ecn); |
| 17174 | EXPECT_EQ(writer_->last_ecn_sent(), ecn); |
| 17175 | } |
| 17176 | } |
| 17177 | |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17178 | TEST_P(QuicConnectionTest, RtoDisablesEcnMarking) { |
| 17179 | SetQuicReloadableFlag(quic_send_ect1, true); |
| 17180 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17181 | TestPerPacketOptions per_packet_options; |
| 17182 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17183 | connection_.set_per_packet_options(&per_packet_options); |
| 17184 | QuicPacketCreatorPeer::SetPacketNumber( |
| 17185 | QuicConnectionPeer::GetPacketCreator(&connection_), 1); |
| 17186 | SendPing(); |
| 17187 | connection_.OnRetransmissionTimeout(); |
| 17188 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_NOT_ECT); |
| 17189 | EXPECT_EQ(per_packet_options.ecn_codepoint, ECN_ECT1); |
| 17190 | // On 2nd RTO, QUIC abandons ECN. |
| 17191 | connection_.OnRetransmissionTimeout(); |
| 17192 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_NOT_ECT); |
| 17193 | EXPECT_EQ(per_packet_options.ecn_codepoint, ECN_NOT_ECT); |
| 17194 | } |
| 17195 | |
| 17196 | TEST_P(QuicConnectionTest, RtoDoesntDisableEcnMarkingIfEcnAcked) { |
| 17197 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17198 | TestPerPacketOptions per_packet_options; |
| 17199 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17200 | connection_.set_per_packet_options(&per_packet_options); |
| 17201 | QuicPacketCreatorPeer::SetPacketNumber( |
| 17202 | QuicConnectionPeer::GetPacketCreator(&connection_), 1); |
| 17203 | if (!GetQuicReloadableFlag(quic_send_ect1)) { |
| 17204 | EXPECT_QUIC_BUG(connection_.OnInFlightEcnPacketAcked(), |
QUICHE team | 3f0c46a | 2023-05-01 22:21:35 -0700 | [diff] [blame] | 17205 | "Unexpected call to OnInFlightEcnPacketAcked\\(\\)"); |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17206 | return; |
| 17207 | } else { |
| 17208 | connection_.OnInFlightEcnPacketAcked(); |
| 17209 | } |
| 17210 | SendPing(); |
| 17211 | // Because an ECN packet was acked, PTOs have no effect on ECN settings. |
| 17212 | connection_.OnRetransmissionTimeout(); |
| 17213 | QuicEcnCodepoint expected_codepoint = |
| 17214 | GetQuicReloadableFlag(quic_send_ect1) ? ECN_ECT1 : ECN_NOT_ECT; |
| 17215 | EXPECT_EQ(writer_->last_ecn_sent(), expected_codepoint); |
| 17216 | EXPECT_EQ(per_packet_options.ecn_codepoint, expected_codepoint); |
| 17217 | connection_.OnRetransmissionTimeout(); |
| 17218 | EXPECT_EQ(writer_->last_ecn_sent(), expected_codepoint); |
| 17219 | EXPECT_EQ(per_packet_options.ecn_codepoint, expected_codepoint); |
| 17220 | } |
| 17221 | |
| 17222 | TEST_P(QuicConnectionTest, InvalidFeedbackCancelsEcn) { |
| 17223 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17224 | TestPerPacketOptions per_packet_options; |
| 17225 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17226 | connection_.set_per_packet_options(&per_packet_options); |
| 17227 | EXPECT_EQ(per_packet_options.ecn_codepoint, ECN_ECT1); |
| 17228 | if (!GetQuicReloadableFlag(quic_send_ect1)) { |
| 17229 | EXPECT_QUIC_BUG(connection_.OnInvalidEcnFeedback(), |
QUICHE team | 3f0c46a | 2023-05-01 22:21:35 -0700 | [diff] [blame] | 17230 | "Unexpected call to OnInvalidEcnFeedback\\(\\)\\."); |
martinduke | d2d9e7b | 2023-04-20 14:13:01 -0700 | [diff] [blame] | 17231 | return; |
| 17232 | } else { |
| 17233 | connection_.OnInvalidEcnFeedback(); |
| 17234 | } |
| 17235 | EXPECT_EQ(per_packet_options.ecn_codepoint, ECN_NOT_ECT); |
| 17236 | } |
| 17237 | |
martinduke | c0da764 | 2023-05-04 17:23:22 -0700 | [diff] [blame] | 17238 | TEST_P(QuicConnectionTest, StateMatchesSentEcn) { |
| 17239 | SetQuicReloadableFlag(quic_send_ect1, true); |
| 17240 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17241 | TestPerPacketOptions per_packet_options; |
| 17242 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17243 | connection_.set_per_packet_options(&per_packet_options); |
| 17244 | SendPing(); |
| 17245 | QuicSentPacketManager* sent_packet_manager = |
| 17246 | QuicConnectionPeer::GetSentPacketManager(&connection_); |
| 17247 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_ECT1); |
| 17248 | EXPECT_EQ( |
| 17249 | QuicSentPacketManagerPeer::GetEct1Sent(sent_packet_manager, INITIAL_DATA), |
| 17250 | 1); |
| 17251 | } |
| 17252 | |
| 17253 | TEST_P(QuicConnectionTest, CoalescedPacketSplitsEcn) { |
| 17254 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 17255 | return; |
| 17256 | } |
| 17257 | SetQuicReloadableFlag(quic_send_ect1, true); |
| 17258 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17259 | TestPerPacketOptions per_packet_options; |
| 17260 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17261 | connection_.set_per_packet_options(&per_packet_options); |
| 17262 | // All these steps are necessary to send an INITIAL ping and save it to be |
| 17263 | // coalesced, instead of just calling SendPing() and sending it immediately. |
| 17264 | char buffer[1000]; |
| 17265 | creator_->set_encryption_level(ENCRYPTION_INITIAL); |
| 17266 | QuicFrames frames; |
| 17267 | QuicPingFrame ping; |
| 17268 | frames.emplace_back(QuicFrame(ping)); |
| 17269 | SerializedPacket packet1 = QuicPacketCreatorPeer::SerializeAllFrames( |
| 17270 | creator_, frames, buffer, sizeof(buffer)); |
| 17271 | connection_.SendOrQueuePacket(std::move(packet1)); |
| 17272 | creator_->set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 17273 | EXPECT_CALL(*send_algorithm_, SupportsECT0()).WillRepeatedly(Return(true)); |
| 17274 | // If not for the line below, these packets would coalesce. |
| 17275 | per_packet_options.ecn_codepoint = ECN_ECT0; |
| 17276 | EXPECT_EQ(writer_->packets_write_attempts(), 0); |
| 17277 | SendPing(); |
| 17278 | EXPECT_EQ(writer_->packets_write_attempts(), 2); |
| 17279 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_ECT0); |
| 17280 | } |
| 17281 | |
| 17282 | TEST_P(QuicConnectionTest, BufferedPacketRetainsOldEcn) { |
| 17283 | SetQuicReloadableFlag(quic_send_ect1, true); |
| 17284 | EXPECT_CALL(*send_algorithm_, SupportsECT1()).WillRepeatedly(Return(true)); |
| 17285 | TestPerPacketOptions per_packet_options; |
| 17286 | per_packet_options.ecn_codepoint = ECN_ECT1; |
| 17287 | connection_.set_per_packet_options(&per_packet_options); |
| 17288 | writer_->SetWriteBlocked(); |
| 17289 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(2); |
| 17290 | SendPing(); |
| 17291 | EXPECT_CALL(*send_algorithm_, SupportsECT0()).WillRepeatedly(Return(true)); |
| 17292 | per_packet_options.ecn_codepoint = ECN_ECT0; |
| 17293 | writer_->SetWritable(); |
| 17294 | connection_.OnCanWrite(); |
| 17295 | EXPECT_EQ(writer_->last_ecn_sent(), ECN_ECT1); |
| 17296 | } |
| 17297 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 17298 | } // namespace |
| 17299 | } // namespace test |
| 17300 | } // namespace quic |