QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_packet_generator.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 22 | #include "net/third_party/quiche/src/quic/test_tools/mock_random.h" |
| 23 | #include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h" |
| 24 | #include "net/third_party/quiche/src/quic/test_tools/quic_packet_creator_peer.h" |
| 25 | #include "net/third_party/quiche/src/quic/test_tools/quic_packet_generator_peer.h" |
| 26 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 27 | #include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h" |
| 28 | #include "net/third_party/quiche/src/quic/test_tools/simple_quic_framer.h" |
| 29 | |
| 30 | using testing::_; |
| 31 | using testing::InSequence; |
| 32 | using testing::Return; |
| 33 | using testing::StrictMock; |
| 34 | |
| 35 | namespace quic { |
| 36 | namespace test { |
| 37 | namespace { |
| 38 | |
| 39 | class MockDelegate : public QuicPacketGenerator::DelegateInterface { |
| 40 | public: |
| 41 | MockDelegate() {} |
| 42 | MockDelegate(const MockDelegate&) = delete; |
| 43 | MockDelegate& operator=(const MockDelegate&) = delete; |
| 44 | ~MockDelegate() override {} |
| 45 | |
| 46 | MOCK_METHOD2(ShouldGeneratePacket, |
| 47 | bool(HasRetransmittableData retransmittable, |
| 48 | IsHandshake handshake)); |
| 49 | MOCK_METHOD0(MaybeBundleAckOpportunistically, const QuicFrames()); |
| 50 | MOCK_METHOD0(GetUpdatedAckFrame, const QuicFrame()); |
| 51 | MOCK_METHOD1(PopulateStopWaitingFrame, void(QuicStopWaitingFrame*)); |
| 52 | MOCK_METHOD0(GetPacketBuffer, char*()); |
| 53 | MOCK_METHOD1(OnSerializedPacket, void(SerializedPacket* packet)); |
| 54 | MOCK_METHOD3(OnUnrecoverableError, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 55 | void(QuicErrorCode, const std::string&, ConnectionCloseSource)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | |
| 57 | void SetCanWriteAnything() { |
| 58 | EXPECT_CALL(*this, ShouldGeneratePacket(_, _)).WillRepeatedly(Return(true)); |
| 59 | EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _)) |
| 60 | .WillRepeatedly(Return(true)); |
| 61 | } |
| 62 | |
| 63 | void SetCanNotWrite() { |
| 64 | EXPECT_CALL(*this, ShouldGeneratePacket(_, _)) |
| 65 | .WillRepeatedly(Return(false)); |
| 66 | EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _)) |
| 67 | .WillRepeatedly(Return(false)); |
| 68 | } |
| 69 | |
| 70 | // Use this when only ack frames should be allowed to be written. |
| 71 | void SetCanWriteOnlyNonRetransmittable() { |
| 72 | EXPECT_CALL(*this, ShouldGeneratePacket(_, _)) |
| 73 | .WillRepeatedly(Return(false)); |
| 74 | EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _)) |
| 75 | .WillRepeatedly(Return(true)); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | // Simple struct for describing the contents of a packet. |
| 80 | // Useful in conjunction with a SimpleQuicFrame for validating that a packet |
| 81 | // contains the expected frames. |
| 82 | struct PacketContents { |
| 83 | PacketContents() |
| 84 | : num_ack_frames(0), |
| 85 | num_connection_close_frames(0), |
| 86 | num_goaway_frames(0), |
| 87 | num_rst_stream_frames(0), |
| 88 | num_stop_waiting_frames(0), |
| 89 | num_stream_frames(0), |
| 90 | num_crypto_frames(0), |
| 91 | num_ping_frames(0), |
| 92 | num_mtu_discovery_frames(0), |
| 93 | num_padding_frames(0) {} |
| 94 | |
| 95 | size_t num_ack_frames; |
| 96 | size_t num_connection_close_frames; |
| 97 | size_t num_goaway_frames; |
| 98 | size_t num_rst_stream_frames; |
| 99 | size_t num_stop_waiting_frames; |
| 100 | size_t num_stream_frames; |
| 101 | size_t num_crypto_frames; |
| 102 | size_t num_ping_frames; |
| 103 | size_t num_mtu_discovery_frames; |
| 104 | size_t num_padding_frames; |
| 105 | }; |
| 106 | |
| 107 | } // namespace |
| 108 | |
| 109 | class TestPacketGenerator : public QuicPacketGenerator { |
| 110 | public: |
| 111 | TestPacketGenerator(QuicConnectionId connection_id, |
| 112 | QuicFramer* framer, |
| 113 | QuicRandom* random_generator, |
| 114 | DelegateInterface* delegate, |
| 115 | SimpleDataProducer* producer) |
| 116 | : QuicPacketGenerator(connection_id, framer, random_generator, delegate), |
| 117 | ack_frame_(InitAckFrame(1)), |
| 118 | delegate_(static_cast<MockDelegate*>(delegate)), |
| 119 | producer_(producer) {} |
| 120 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 121 | bool ConsumeRetransmittableControlFrame(const QuicFrame& frame, |
| 122 | bool bundle_ack) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode) && |
| 124 | !QuicPacketGeneratorPeer::GetPacketCreator(this)->has_ack()) { |
| 125 | QuicFrames frames; |
| 126 | if (bundle_ack) { |
| 127 | frames.push_back(QuicFrame(&ack_frame_)); |
| 128 | } |
| 129 | if (delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 130 | NOT_HANDSHAKE)) { |
| 131 | EXPECT_CALL(*delegate_, MaybeBundleAckOpportunistically()) |
| 132 | .WillOnce(Return(frames)); |
| 133 | } |
| 134 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 135 | return QuicPacketGenerator::ConsumeRetransmittableControlFrame(frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | QuicConsumedData ConsumeDataFastPath(QuicStreamId id, |
| 139 | const struct iovec* iov, |
| 140 | int iov_count, |
| 141 | size_t total_length, |
| 142 | QuicStreamOffset offset, |
| 143 | bool fin) { |
| 144 | // Save data before data is consumed. |
| 145 | if (total_length > 0) { |
| 146 | producer_->SaveStreamData(id, iov, iov_count, 0, total_length); |
| 147 | } |
| 148 | return QuicPacketGenerator::ConsumeDataFastPath(id, total_length, offset, |
| 149 | fin, 0); |
| 150 | } |
| 151 | |
| 152 | QuicConsumedData ConsumeData(QuicStreamId id, |
| 153 | const struct iovec* iov, |
| 154 | int iov_count, |
| 155 | size_t total_length, |
| 156 | QuicStreamOffset offset, |
| 157 | StreamSendingState state) { |
| 158 | // Save data before data is consumed. |
| 159 | if (total_length > 0) { |
| 160 | producer_->SaveStreamData(id, iov, iov_count, 0, total_length); |
| 161 | } |
| 162 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode) && |
| 163 | !QuicPacketGeneratorPeer::GetPacketCreator(this)->has_ack() && |
| 164 | delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 165 | NOT_HANDSHAKE)) { |
| 166 | EXPECT_CALL(*delegate_, MaybeBundleAckOpportunistically()).Times(1); |
| 167 | } |
| 168 | return QuicPacketGenerator::ConsumeData(id, total_length, offset, state); |
| 169 | } |
| 170 | |
| 171 | MessageStatus AddMessageFrame(QuicMessageId message_id, |
| 172 | QuicMemSliceSpan message) { |
| 173 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode) && |
| 174 | !QuicPacketGeneratorPeer::GetPacketCreator(this)->has_ack() && |
| 175 | delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 176 | NOT_HANDSHAKE)) { |
| 177 | EXPECT_CALL(*delegate_, MaybeBundleAckOpportunistically()).Times(1); |
| 178 | } |
| 179 | return QuicPacketGenerator::AddMessageFrame(message_id, message); |
| 180 | } |
| 181 | |
| 182 | size_t ConsumeCryptoData(EncryptionLevel level, |
| 183 | QuicStringPiece data, |
| 184 | QuicStreamOffset offset) { |
| 185 | producer_->SaveCryptoData(level, offset, data); |
| 186 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode) && |
| 187 | !QuicPacketGeneratorPeer::GetPacketCreator(this)->has_ack() && |
| 188 | delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 189 | NOT_HANDSHAKE)) { |
| 190 | EXPECT_CALL(*delegate_, MaybeBundleAckOpportunistically()).Times(1); |
| 191 | } |
| 192 | return QuicPacketGenerator::ConsumeCryptoData(level, data.length(), offset); |
| 193 | } |
| 194 | |
| 195 | QuicAckFrame ack_frame_; |
| 196 | MockDelegate* delegate_; |
| 197 | SimpleDataProducer* producer_; |
| 198 | }; |
| 199 | |
| 200 | class QuicPacketGeneratorTest : public QuicTest { |
| 201 | public: |
| 202 | QuicPacketGeneratorTest() |
| 203 | : framer_(AllSupportedVersions(), |
| 204 | QuicTime::Zero(), |
| 205 | Perspective::IS_CLIENT, |
| 206 | kQuicDefaultConnectionIdLength), |
| 207 | generator_(TestConnectionId(), |
| 208 | &framer_, |
| 209 | &random_generator_, |
| 210 | &delegate_, |
| 211 | &producer_), |
| 212 | creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)), |
| 213 | ack_frame_(InitAckFrame(1)) { |
| 214 | EXPECT_CALL(delegate_, GetPacketBuffer()).WillRepeatedly(Return(nullptr)); |
| 215 | creator_->SetEncrypter( |
| 216 | ENCRYPTION_FORWARD_SECURE, |
| 217 | QuicMakeUnique<NullEncrypter>(Perspective::IS_CLIENT)); |
| 218 | creator_->set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 219 | framer_.set_data_producer(&producer_); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 220 | if (simple_framer_.framer()->version().KnowsWhichDecrypterToUse()) { |
| 221 | simple_framer_.framer()->InstallDecrypter( |
| 222 | ENCRYPTION_FORWARD_SECURE, |
| 223 | QuicMakeUnique<NullDecrypter>(Perspective::IS_SERVER)); |
| 224 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 225 | generator_.AttachPacketFlusher(); |
| 226 | } |
| 227 | |
| 228 | ~QuicPacketGeneratorTest() override { |
| 229 | for (SerializedPacket& packet : packets_) { |
| 230 | delete[] packet.encrypted_buffer; |
| 231 | ClearSerializedPacket(&packet); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void SavePacket(SerializedPacket* packet) { |
| 236 | packet->encrypted_buffer = CopyBuffer(*packet); |
| 237 | packets_.push_back(*packet); |
| 238 | packet->encrypted_buffer = nullptr; |
| 239 | packet->retransmittable_frames.clear(); |
| 240 | } |
| 241 | |
| 242 | protected: |
| 243 | QuicRstStreamFrame* CreateRstStreamFrame() { |
| 244 | return new QuicRstStreamFrame(1, 1, QUIC_STREAM_NO_ERROR, 0); |
| 245 | } |
| 246 | |
| 247 | QuicGoAwayFrame* CreateGoAwayFrame() { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 248 | return new QuicGoAwayFrame(2, QUIC_NO_ERROR, 1, std::string()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void CheckPacketContains(const PacketContents& contents, |
| 252 | size_t packet_index) { |
| 253 | ASSERT_GT(packets_.size(), packet_index); |
| 254 | const SerializedPacket& packet = packets_[packet_index]; |
| 255 | size_t num_retransmittable_frames = |
| 256 | contents.num_connection_close_frames + contents.num_goaway_frames + |
| 257 | contents.num_rst_stream_frames + contents.num_stream_frames + |
| 258 | contents.num_crypto_frames + contents.num_ping_frames; |
| 259 | size_t num_frames = |
| 260 | contents.num_ack_frames + contents.num_stop_waiting_frames + |
| 261 | contents.num_mtu_discovery_frames + contents.num_padding_frames + |
| 262 | num_retransmittable_frames; |
| 263 | |
| 264 | if (num_retransmittable_frames == 0) { |
| 265 | ASSERT_TRUE(packet.retransmittable_frames.empty()); |
| 266 | } else { |
| 267 | ASSERT_FALSE(packet.retransmittable_frames.empty()); |
| 268 | EXPECT_EQ(num_retransmittable_frames, |
| 269 | packet.retransmittable_frames.size()); |
| 270 | } |
| 271 | |
| 272 | ASSERT_TRUE(packet.encrypted_buffer != nullptr); |
| 273 | ASSERT_TRUE(simple_framer_.ProcessPacket( |
| 274 | QuicEncryptedPacket(packet.encrypted_buffer, packet.encrypted_length))); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 275 | size_t num_padding_frames = 0; |
| 276 | if (contents.num_padding_frames == 0) { |
| 277 | num_padding_frames = simple_framer_.padding_frames().size(); |
| 278 | } |
| 279 | EXPECT_EQ(num_frames + num_padding_frames, simple_framer_.num_frames()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 280 | EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size()); |
| 281 | EXPECT_EQ(contents.num_connection_close_frames, |
| 282 | simple_framer_.connection_close_frames().size()); |
| 283 | EXPECT_EQ(contents.num_goaway_frames, |
| 284 | simple_framer_.goaway_frames().size()); |
| 285 | EXPECT_EQ(contents.num_rst_stream_frames, |
| 286 | simple_framer_.rst_stream_frames().size()); |
| 287 | EXPECT_EQ(contents.num_stream_frames, |
| 288 | simple_framer_.stream_frames().size()); |
| 289 | EXPECT_EQ(contents.num_crypto_frames, |
| 290 | simple_framer_.crypto_frames().size()); |
| 291 | EXPECT_EQ(contents.num_stop_waiting_frames, |
| 292 | simple_framer_.stop_waiting_frames().size()); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 293 | if (contents.num_padding_frames != 0) { |
| 294 | EXPECT_EQ(contents.num_padding_frames, |
| 295 | simple_framer_.padding_frames().size()); |
| 296 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 297 | |
| 298 | // From the receiver's perspective, MTU discovery frames are ping frames. |
| 299 | EXPECT_EQ(contents.num_ping_frames + contents.num_mtu_discovery_frames, |
| 300 | simple_framer_.ping_frames().size()); |
| 301 | } |
| 302 | |
| 303 | void CheckPacketHasSingleStreamFrame(size_t packet_index) { |
| 304 | ASSERT_GT(packets_.size(), packet_index); |
| 305 | const SerializedPacket& packet = packets_[packet_index]; |
| 306 | ASSERT_FALSE(packet.retransmittable_frames.empty()); |
| 307 | EXPECT_EQ(1u, packet.retransmittable_frames.size()); |
| 308 | ASSERT_TRUE(packet.encrypted_buffer != nullptr); |
| 309 | ASSERT_TRUE(simple_framer_.ProcessPacket( |
| 310 | QuicEncryptedPacket(packet.encrypted_buffer, packet.encrypted_length))); |
| 311 | EXPECT_EQ(1u, simple_framer_.num_frames()); |
| 312 | EXPECT_EQ(1u, simple_framer_.stream_frames().size()); |
| 313 | } |
| 314 | |
| 315 | void CheckAllPacketsHaveSingleStreamFrame() { |
| 316 | for (size_t i = 0; i < packets_.size(); i++) { |
| 317 | CheckPacketHasSingleStreamFrame(i); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void CreateData(size_t len) { |
| 322 | data_array_.reset(new char[len]); |
| 323 | memset(data_array_.get(), '?', len); |
| 324 | iov_.iov_base = data_array_.get(); |
| 325 | iov_.iov_len = len; |
| 326 | } |
| 327 | |
| 328 | QuicFramer framer_; |
| 329 | MockRandom random_generator_; |
| 330 | StrictMock<MockDelegate> delegate_; |
| 331 | TestPacketGenerator generator_; |
| 332 | QuicPacketCreator* creator_; |
| 333 | SimpleQuicFramer simple_framer_; |
| 334 | std::vector<SerializedPacket> packets_; |
| 335 | QuicAckFrame ack_frame_; |
| 336 | struct iovec iov_; |
| 337 | SimpleBufferAllocator allocator_; |
| 338 | |
| 339 | private: |
| 340 | std::unique_ptr<char[]> data_array_; |
| 341 | SimpleDataProducer producer_; |
| 342 | }; |
| 343 | |
| 344 | class MockDebugDelegate : public QuicPacketCreator::DebugDelegate { |
| 345 | public: |
| 346 | MOCK_METHOD1(OnFrameAddedToPacket, void(const QuicFrame&)); |
| 347 | }; |
| 348 | |
| 349 | TEST_F(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) { |
| 350 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 351 | return; |
| 352 | } |
| 353 | delegate_.SetCanNotWrite(); |
| 354 | |
| 355 | generator_.SetShouldSendAck(false); |
| 356 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 357 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 358 | } |
| 359 | |
| 360 | TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) { |
| 361 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 362 | return; |
| 363 | } |
| 364 | StrictMock<MockDebugDelegate> debug_delegate; |
| 365 | |
| 366 | generator_.set_debug_delegate(&debug_delegate); |
| 367 | delegate_.SetCanWriteOnlyNonRetransmittable(); |
| 368 | |
| 369 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 370 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 371 | EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1); |
| 372 | |
| 373 | generator_.SetShouldSendAck(false); |
| 374 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 375 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 376 | } |
| 377 | |
| 378 | TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) { |
| 379 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 380 | return; |
| 381 | } |
| 382 | delegate_.SetCanWriteOnlyNonRetransmittable(); |
| 383 | |
| 384 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 385 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 386 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 387 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 388 | |
| 389 | generator_.SetShouldSendAck(false); |
| 390 | generator_.Flush(); |
| 391 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 392 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 393 | |
| 394 | PacketContents contents; |
| 395 | contents.num_ack_frames = 1; |
| 396 | CheckPacketContains(contents, 0); |
| 397 | } |
| 398 | |
| 399 | TEST_F(QuicPacketGeneratorTest, ShouldSendAck_MultipleCalls) { |
| 400 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 401 | return; |
| 402 | } |
| 403 | // Make sure that calling SetShouldSendAck multiple times does not result in a |
| 404 | // crash. Previously this would result in multiple QuicFrames queued in the |
| 405 | // packet generator, with all but the last with internal pointers to freed |
| 406 | // memory. |
| 407 | delegate_.SetCanWriteAnything(); |
| 408 | |
| 409 | // Only one AckFrame should be created. |
| 410 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 411 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 412 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 413 | .Times(1) |
| 414 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 415 | |
| 416 | generator_.SetShouldSendAck(false); |
| 417 | generator_.SetShouldSendAck(false); |
| 418 | generator_.Flush(); |
| 419 | } |
| 420 | |
| 421 | TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritable) { |
| 422 | delegate_.SetCanNotWrite(); |
| 423 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 424 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 425 | const bool consumed = |
| 426 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 427 | /*bundle_ack=*/false); |
| 428 | if (generator_.deprecate_queued_control_frames()) { |
| 429 | EXPECT_FALSE(consumed); |
| 430 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 431 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 432 | delete rst_frame; |
| 433 | } else { |
| 434 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 435 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 436 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | TEST_F(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) { |
| 440 | delegate_.SetCanWriteOnlyNonRetransmittable(); |
| 441 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 442 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 443 | const bool consumed = |
| 444 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 445 | /*bundle_ack=*/false); |
| 446 | if (generator_.deprecate_queued_control_frames()) { |
| 447 | EXPECT_FALSE(consumed); |
| 448 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 449 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 450 | delete rst_frame; |
| 451 | } else { |
| 452 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 453 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 454 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) { |
| 458 | delegate_.SetCanWriteAnything(); |
| 459 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 460 | generator_.ConsumeRetransmittableControlFrame( |
| 461 | QuicFrame(CreateRstStreamFrame()), |
| 462 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 463 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 464 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 465 | } |
| 466 | |
| 467 | TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) { |
| 468 | delegate_.SetCanNotWrite(); |
| 469 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 470 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 471 | const bool consumed = |
| 472 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 473 | /*bundle_ack=*/false); |
| 474 | if (generator_.deprecate_queued_control_frames()) { |
| 475 | EXPECT_FALSE(consumed); |
| 476 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 477 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 478 | delete rst_frame; |
| 479 | return; |
| 480 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 481 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 482 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 483 | generator_.Flush(); |
| 484 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 485 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 486 | |
| 487 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 488 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 489 | generator_.AttachPacketFlusher(); |
| 490 | generator_.FlushAllQueuedFrames(); |
| 491 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 492 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 493 | |
| 494 | PacketContents contents; |
| 495 | contents.num_rst_stream_frames = 1; |
| 496 | CheckPacketContains(contents, 0); |
| 497 | } |
| 498 | |
| 499 | TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) { |
| 500 | delegate_.SetCanWriteAnything(); |
| 501 | |
| 502 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 503 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 504 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 505 | generator_.ConsumeRetransmittableControlFrame( |
| 506 | QuicFrame(CreateRstStreamFrame()), |
| 507 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 508 | generator_.Flush(); |
| 509 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 510 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 511 | |
| 512 | PacketContents contents; |
| 513 | contents.num_rst_stream_frames = 1; |
| 514 | CheckPacketContains(contents, 0); |
| 515 | } |
| 516 | |
| 517 | TEST_F(QuicPacketGeneratorTest, ConsumeCryptoData) { |
| 518 | delegate_.SetCanWriteAnything(); |
| 519 | |
| 520 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 521 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 522 | std::string data = "crypto data"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 523 | size_t consumed_bytes = |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 524 | generator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data, 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 525 | generator_.Flush(); |
| 526 | EXPECT_EQ(data.length(), consumed_bytes); |
| 527 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 528 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 529 | |
| 530 | PacketContents contents; |
| 531 | contents.num_crypto_frames = 1; |
| 532 | contents.num_padding_frames = 1; |
| 533 | CheckPacketContains(contents, 0); |
| 534 | } |
| 535 | |
| 536 | TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) { |
| 537 | delegate_.SetCanNotWrite(); |
| 538 | |
| 539 | MakeIOVector("foo", &iov_); |
| 540 | QuicConsumedData consumed = generator_.ConsumeData( |
| 541 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 542 | iov_.iov_len, 0, FIN); |
| 543 | EXPECT_EQ(0u, consumed.bytes_consumed); |
| 544 | EXPECT_FALSE(consumed.fin_consumed); |
| 545 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 546 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 547 | } |
| 548 | |
| 549 | TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) { |
| 550 | delegate_.SetCanWriteAnything(); |
| 551 | |
| 552 | MakeIOVector("foo", &iov_); |
| 553 | QuicConsumedData consumed = generator_.ConsumeData( |
| 554 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 555 | iov_.iov_len, 0, FIN); |
| 556 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 557 | EXPECT_TRUE(consumed.fin_consumed); |
| 558 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 559 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 560 | } |
| 561 | |
| 562 | TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) { |
| 563 | delegate_.SetCanWriteAnything(); |
| 564 | |
| 565 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 566 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 567 | MakeIOVector("foo", &iov_); |
| 568 | QuicConsumedData consumed = generator_.ConsumeData( |
| 569 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 570 | iov_.iov_len, 0, FIN); |
| 571 | generator_.Flush(); |
| 572 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 573 | EXPECT_TRUE(consumed.fin_consumed); |
| 574 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 575 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 576 | |
| 577 | PacketContents contents; |
| 578 | contents.num_stream_frames = 1; |
| 579 | CheckPacketContains(contents, 0); |
| 580 | } |
| 581 | |
| 582 | // Test the behavior of ConsumeData when the data consumed is for the crypto |
| 583 | // handshake stream. Ensure that the packet is always sent and padded even if |
| 584 | // the generator operates in batch mode. |
| 585 | TEST_F(QuicPacketGeneratorTest, ConsumeData_Handshake) { |
| 586 | delegate_.SetCanWriteAnything(); |
| 587 | |
| 588 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 589 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 590 | MakeIOVector("foo bar", &iov_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 591 | QuicConsumedData consumed = generator_.ConsumeData( |
| 592 | QuicUtils::GetCryptoStreamId(framer_.transport_version()), &iov_, 1u, |
| 593 | iov_.iov_len, 0, NO_FIN); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 594 | EXPECT_EQ(7u, consumed.bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 595 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 596 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 597 | |
| 598 | PacketContents contents; |
| 599 | contents.num_stream_frames = 1; |
| 600 | contents.num_padding_frames = 1; |
| 601 | CheckPacketContains(contents, 0); |
| 602 | |
| 603 | ASSERT_EQ(1u, packets_.size()); |
| 604 | ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength()); |
| 605 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 606 | } |
| 607 | |
| 608 | // Test the behavior of ConsumeData when the data is for the crypto handshake |
| 609 | // stream, but padding is disabled. |
| 610 | TEST_F(QuicPacketGeneratorTest, ConsumeData_Handshake_PaddingDisabled) { |
| 611 | generator_.set_fully_pad_crypto_hadshake_packets(false); |
| 612 | |
| 613 | delegate_.SetCanWriteAnything(); |
| 614 | |
| 615 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 616 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame^] | 617 | std::string data = "foo"; |
| 618 | MakeIOVector(data, &iov_); |
| 619 | size_t bytes_consumed = 0; |
| 620 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 621 | bytes_consumed = generator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data, 0); |
| 622 | } else { |
| 623 | bytes_consumed = |
| 624 | generator_ |
| 625 | .ConsumeData( |
| 626 | QuicUtils::GetCryptoStreamId(framer_.transport_version()), |
| 627 | &iov_, 1u, iov_.iov_len, 0, NO_FIN) |
| 628 | .bytes_consumed; |
| 629 | } |
| 630 | EXPECT_EQ(3u, bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 631 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 632 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 633 | |
| 634 | PacketContents contents; |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame^] | 635 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 636 | contents.num_crypto_frames = 1; |
| 637 | } else { |
| 638 | contents.num_stream_frames = 1; |
| 639 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 640 | contents.num_padding_frames = 0; |
| 641 | CheckPacketContains(contents, 0); |
| 642 | |
| 643 | ASSERT_EQ(1u, packets_.size()); |
| 644 | |
| 645 | // Packet is not fully padded, but we want to future packets to be larger. |
| 646 | ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength()); |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame^] | 647 | size_t expected_packet_length = 27; |
| 648 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 649 | // The framing of CRYPTO frames is slightly different than that of stream |
| 650 | // frames, so the expected packet length differs slightly. |
| 651 | expected_packet_length = 28; |
| 652 | } |
| 653 | EXPECT_EQ(expected_packet_length, packets_[0].encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) { |
| 657 | delegate_.SetCanWriteAnything(); |
| 658 | |
| 659 | EXPECT_QUIC_BUG(generator_.ConsumeData(QuicUtils::GetHeadersStreamId( |
| 660 | framer_.transport_version()), |
| 661 | nullptr, 0, 0, 0, NO_FIN), |
| 662 | "Attempt to consume empty data without FIN."); |
| 663 | } |
| 664 | |
| 665 | TEST_F(QuicPacketGeneratorTest, |
| 666 | ConsumeDataMultipleTimes_WritableAndShouldNotFlush) { |
| 667 | delegate_.SetCanWriteAnything(); |
| 668 | |
| 669 | MakeIOVector("foo", &iov_); |
| 670 | generator_.ConsumeData( |
| 671 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 672 | iov_.iov_len, 0, FIN); |
| 673 | MakeIOVector("quux", &iov_); |
| 674 | QuicConsumedData consumed = |
| 675 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 3, NO_FIN); |
| 676 | EXPECT_EQ(4u, consumed.bytes_consumed); |
| 677 | EXPECT_FALSE(consumed.fin_consumed); |
| 678 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 679 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 680 | } |
| 681 | |
| 682 | TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) { |
| 683 | delegate_.SetCanWriteAnything(); |
| 684 | |
| 685 | MakeIOVector("foo", &iov_); |
| 686 | generator_.ConsumeData( |
| 687 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 688 | iov_.iov_len, 0, FIN); |
| 689 | MakeIOVector("quux", &iov_); |
| 690 | QuicConsumedData consumed = generator_.ConsumeData( |
| 691 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 692 | iov_.iov_len, 3, NO_FIN); |
| 693 | EXPECT_EQ(4u, consumed.bytes_consumed); |
| 694 | EXPECT_FALSE(consumed.fin_consumed); |
| 695 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 696 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 697 | |
| 698 | // Now both frames will be flushed out. |
| 699 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 700 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 701 | generator_.Flush(); |
| 702 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 703 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 704 | |
| 705 | PacketContents contents; |
| 706 | contents.num_stream_frames = 2; |
| 707 | CheckPacketContains(contents, 0); |
| 708 | } |
| 709 | |
| 710 | TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) { |
| 711 | // Set the packet size be enough for two stream frames with 0 stream offset, |
| 712 | // but not enough for a stream frame of 0 offset and one with non-zero offset. |
| 713 | size_t length = |
| 714 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 715 | GetPacketHeaderSize( |
| 716 | framer_.transport_version(), |
| 717 | creator_->GetDestinationConnectionIdLength(), |
| 718 | creator_->GetSourceConnectionIdLength(), |
| 719 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 720 | !kIncludeDiversificationNonce, |
| 721 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 722 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 723 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 724 | // Add an extra 3 bytes for the payload and 1 byte so |
| 725 | // BytesFree is larger than the GetMinStreamFrameSize. |
| 726 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), 1, 0, |
| 727 | false, 3) + |
| 728 | 3 + |
| 729 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), 1, 0, true, |
| 730 | 1) + |
| 731 | 1; |
| 732 | generator_.SetMaxPacketLength(length); |
| 733 | delegate_.SetCanWriteAnything(); |
| 734 | { |
| 735 | InSequence dummy; |
| 736 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 737 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 738 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 739 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 740 | } |
| 741 | // Queue enough data to prevent a stream frame with a non-zero offset from |
| 742 | // fitting. |
| 743 | MakeIOVector("foo", &iov_); |
| 744 | QuicConsumedData consumed = generator_.ConsumeData( |
| 745 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 746 | iov_.iov_len, 0, NO_FIN); |
| 747 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 748 | EXPECT_FALSE(consumed.fin_consumed); |
| 749 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 750 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 751 | |
| 752 | // This frame will not fit with the existing frame, causing the queued frame |
| 753 | // to be serialized, and it will be added to a new open packet. |
| 754 | MakeIOVector("bar", &iov_); |
| 755 | consumed = generator_.ConsumeData( |
| 756 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 757 | iov_.iov_len, 3, FIN); |
| 758 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 759 | EXPECT_TRUE(consumed.fin_consumed); |
| 760 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 761 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 762 | |
| 763 | creator_->Flush(); |
| 764 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 765 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 766 | |
| 767 | PacketContents contents; |
| 768 | contents.num_stream_frames = 1; |
| 769 | CheckPacketContains(contents, 0); |
| 770 | CheckPacketContains(contents, 1); |
| 771 | } |
| 772 | |
| 773 | TEST_F(QuicPacketGeneratorTest, ConsumeDataFastPath) { |
| 774 | delegate_.SetCanWriteAnything(); |
| 775 | generator_.SetCanSetTransmissionType(true); |
| 776 | generator_.SetTransmissionType(LOSS_RETRANSMISSION); |
| 777 | |
| 778 | // Create a 10000 byte IOVector. |
| 779 | CreateData(10000); |
| 780 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 781 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 782 | QuicConsumedData consumed = generator_.ConsumeDataFastPath( |
| 783 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 784 | iov_.iov_len, 0, true); |
| 785 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 786 | EXPECT_TRUE(consumed.fin_consumed); |
| 787 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 788 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 789 | |
| 790 | PacketContents contents; |
| 791 | contents.num_stream_frames = 1; |
| 792 | CheckPacketContains(contents, 0); |
| 793 | EXPECT_FALSE(packets_.empty()); |
| 794 | SerializedPacket packet = packets_.back(); |
| 795 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 796 | EXPECT_EQ(LOSS_RETRANSMISSION, packet.transmission_type); |
| 797 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 798 | const QuicStreamFrame& stream_frame = |
| 799 | packet.retransmittable_frames.front().stream_frame; |
| 800 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 801 | } |
| 802 | |
| 803 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLarge) { |
| 804 | delegate_.SetCanWriteAnything(); |
| 805 | |
| 806 | // Create a 10000 byte IOVector. |
| 807 | CreateData(10000); |
| 808 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 809 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 810 | QuicConsumedData consumed = generator_.ConsumeData( |
| 811 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 812 | iov_.iov_len, 0, FIN); |
| 813 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 814 | EXPECT_TRUE(consumed.fin_consumed); |
| 815 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 816 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 817 | |
| 818 | PacketContents contents; |
| 819 | contents.num_stream_frames = 1; |
| 820 | CheckPacketContains(contents, 0); |
| 821 | EXPECT_FALSE(packets_.empty()); |
| 822 | SerializedPacket packet = packets_.back(); |
| 823 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 824 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 825 | const QuicStreamFrame& stream_frame = |
| 826 | packet.retransmittable_frames.front().stream_frame; |
| 827 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 828 | } |
| 829 | |
| 830 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLargeSendAckFalse) { |
| 831 | delegate_.SetCanNotWrite(); |
| 832 | |
| 833 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 834 | generator_.SetShouldSendAck(false); |
| 835 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 836 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 837 | const bool success = |
| 838 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 839 | /*bundle_ack=*/true); |
| 840 | if (generator_.deprecate_queued_control_frames()) { |
| 841 | EXPECT_FALSE(success); |
| 842 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 843 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 844 | } else { |
| 845 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 846 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 847 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 848 | |
| 849 | delegate_.SetCanWriteAnything(); |
| 850 | |
| 851 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 852 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 853 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 854 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 855 | if (generator_.deprecate_queued_control_frames()) { |
| 856 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 857 | /*bundle_ack=*/false); |
| 858 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 859 | |
| 860 | // Create a 10000 byte IOVector. |
| 861 | CreateData(10000); |
| 862 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 863 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 864 | if (generator_.deprecate_queued_control_frames()) { |
| 865 | generator_.ConsumeRetransmittableControlFrame( |
| 866 | QuicFrame(CreateRstStreamFrame()), |
| 867 | /*bundle_ack=*/true); |
| 868 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 869 | QuicConsumedData consumed = generator_.ConsumeData( |
| 870 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 871 | iov_.iov_len, 0, FIN); |
| 872 | generator_.Flush(); |
| 873 | |
| 874 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 875 | EXPECT_TRUE(consumed.fin_consumed); |
| 876 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 877 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 878 | |
| 879 | EXPECT_FALSE(packets_.empty()); |
| 880 | SerializedPacket packet = packets_.back(); |
| 881 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 882 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 883 | const QuicStreamFrame& stream_frame = |
| 884 | packet.retransmittable_frames.front().stream_frame; |
| 885 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 886 | } |
| 887 | |
| 888 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLargeSendAckTrue) { |
| 889 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 890 | return; |
| 891 | } |
| 892 | delegate_.SetCanNotWrite(); |
| 893 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 894 | generator_.SetShouldSendAck(true /* stop_waiting */); |
| 895 | } |
| 896 | delegate_.SetCanWriteAnything(); |
| 897 | |
| 898 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 899 | // Set up frames to write into the creator when control frames are written. |
| 900 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 901 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 902 | EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_)); |
| 903 | // Generator should have queued control frames, and creator should be empty. |
| 904 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 905 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 906 | EXPECT_FALSE(creator_->HasPendingFrames()); |
| 907 | } |
| 908 | |
| 909 | // Create a 10000 byte IOVector. |
| 910 | CreateData(10000); |
| 911 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 912 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 913 | QuicConsumedData consumed = generator_.ConsumeData( |
| 914 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 915 | iov_.iov_len, 0, FIN); |
| 916 | generator_.Flush(); |
| 917 | |
| 918 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 919 | EXPECT_TRUE(consumed.fin_consumed); |
| 920 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 921 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 922 | |
| 923 | EXPECT_FALSE(packets_.empty()); |
| 924 | SerializedPacket packet = packets_.back(); |
| 925 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 926 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 927 | const QuicStreamFrame& stream_frame = |
| 928 | packet.retransmittable_frames.front().stream_frame; |
| 929 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 930 | } |
| 931 | |
| 932 | TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) { |
| 933 | delegate_.SetCanNotWrite(); |
| 934 | |
| 935 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 936 | generator_.SetShouldSendAck(false); |
| 937 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 938 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 939 | const bool consumed = |
| 940 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 941 | /*bundle_ack=*/true); |
| 942 | if (generator_.deprecate_queued_control_frames()) { |
| 943 | EXPECT_FALSE(consumed); |
| 944 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 945 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 946 | } else { |
| 947 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 948 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 949 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 950 | EXPECT_FALSE(generator_.HasPendingStreamFramesOfStream(3)); |
| 951 | |
| 952 | delegate_.SetCanWriteAnything(); |
| 953 | |
| 954 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 955 | // When the first write operation is invoked, the ack frame will be |
| 956 | // returned. |
| 957 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 958 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 959 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 960 | if (generator_.deprecate_queued_control_frames()) { |
| 961 | EXPECT_TRUE( |
| 962 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 963 | /*bundle_ack=*/false)); |
| 964 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 965 | // Send some data and a control frame |
| 966 | MakeIOVector("quux", &iov_); |
| 967 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 968 | if (framer_.transport_version() != QUIC_VERSION_99) { |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 969 | generator_.ConsumeRetransmittableControlFrame( |
| 970 | QuicFrame(CreateGoAwayFrame()), |
| 971 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 972 | } |
| 973 | EXPECT_TRUE(generator_.HasPendingStreamFramesOfStream(3)); |
| 974 | |
| 975 | // All five frames will be flushed out in a single packet. |
| 976 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 977 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 978 | generator_.Flush(); |
| 979 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 980 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 981 | EXPECT_FALSE(generator_.HasPendingStreamFramesOfStream(3)); |
| 982 | |
| 983 | PacketContents contents; |
| 984 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 985 | // ACK will be flushed by connection. |
| 986 | contents.num_ack_frames = 0; |
| 987 | } else { |
| 988 | contents.num_ack_frames = 1; |
| 989 | } |
| 990 | if (framer_.transport_version() != QUIC_VERSION_99) { |
| 991 | contents.num_goaway_frames = 1; |
| 992 | } else { |
| 993 | contents.num_goaway_frames = 0; |
| 994 | } |
| 995 | contents.num_rst_stream_frames = 1; |
| 996 | contents.num_stream_frames = 1; |
| 997 | CheckPacketContains(contents, 0); |
| 998 | } |
| 999 | |
| 1000 | TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) { |
| 1001 | delegate_.SetCanNotWrite(); |
| 1002 | |
| 1003 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1004 | generator_.SetShouldSendAck(false); |
| 1005 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1006 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 1007 | const bool success = |
| 1008 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 1009 | /*bundle_ack=*/true); |
| 1010 | if (generator_.deprecate_queued_control_frames()) { |
| 1011 | EXPECT_FALSE(success); |
| 1012 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1013 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1014 | } else { |
| 1015 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1016 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1017 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1018 | |
| 1019 | delegate_.SetCanWriteAnything(); |
| 1020 | |
| 1021 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1022 | // When the first write operation is invoked, the ack frame will be |
| 1023 | // returned. |
| 1024 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 1025 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 1026 | } |
| 1027 | |
| 1028 | { |
| 1029 | InSequence dummy; |
| 1030 | // All five frames will be flushed out in a single packet |
| 1031 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1032 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1033 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1034 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1035 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1036 | if (generator_.deprecate_queued_control_frames()) { |
| 1037 | EXPECT_TRUE( |
| 1038 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 1039 | /*bundle_ack=*/false)); |
| 1040 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1041 | // Send enough data to exceed one packet |
| 1042 | size_t data_len = kDefaultMaxPacketSize + 100; |
| 1043 | CreateData(data_len); |
| 1044 | QuicConsumedData consumed = |
| 1045 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 0, FIN); |
| 1046 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1047 | EXPECT_TRUE(consumed.fin_consumed); |
| 1048 | if (framer_.transport_version() != QUIC_VERSION_99) { |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1049 | generator_.ConsumeRetransmittableControlFrame( |
| 1050 | QuicFrame(CreateGoAwayFrame()), |
| 1051 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | generator_.Flush(); |
| 1055 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1056 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1057 | |
| 1058 | // The first packet should have the queued data and part of the stream data. |
| 1059 | PacketContents contents; |
| 1060 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1061 | // ACK will be sent by connection. |
| 1062 | contents.num_ack_frames = 0; |
| 1063 | } else { |
| 1064 | contents.num_ack_frames = 1; |
| 1065 | } |
| 1066 | contents.num_rst_stream_frames = 1; |
| 1067 | contents.num_stream_frames = 1; |
| 1068 | CheckPacketContains(contents, 0); |
| 1069 | |
| 1070 | // The second should have the remainder of the stream data. |
| 1071 | PacketContents contents2; |
| 1072 | if (framer_.transport_version() != QUIC_VERSION_99) { |
| 1073 | contents2.num_goaway_frames = 1; |
| 1074 | } else { |
| 1075 | contents2.num_goaway_frames = 0; |
| 1076 | } |
| 1077 | contents2.num_stream_frames = 1; |
| 1078 | CheckPacketContains(contents2, 1); |
| 1079 | } |
| 1080 | |
| 1081 | // Regression test of b/120493795. |
| 1082 | TEST_F(QuicPacketGeneratorTest, PacketTransmissionType) { |
| 1083 | delegate_.SetCanWriteAnything(); |
| 1084 | generator_.SetCanSetTransmissionType(true); |
| 1085 | |
| 1086 | // The first ConsumeData will fill the packet without flush. |
| 1087 | generator_.SetTransmissionType(LOSS_RETRANSMISSION); |
| 1088 | |
| 1089 | size_t data_len = 1324; |
| 1090 | CreateData(data_len); |
| 1091 | QuicStreamId stream1_id = |
| 1092 | QuicUtils::GetHeadersStreamId(framer_.transport_version()); |
| 1093 | QuicConsumedData consumed = |
| 1094 | generator_.ConsumeData(stream1_id, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 1095 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1096 | ASSERT_EQ(0u, creator_->BytesFree()) |
| 1097 | << "Test setup failed: Please increase data_len to " |
| 1098 | << data_len + creator_->BytesFree() << " bytes."; |
| 1099 | |
| 1100 | // The second ConsumeData can not be added to the packet and will flush. |
| 1101 | generator_.SetTransmissionType(NOT_RETRANSMISSION); |
| 1102 | |
| 1103 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1104 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1105 | |
| 1106 | QuicStreamId stream2_id = stream1_id + 4; |
| 1107 | |
| 1108 | consumed = |
| 1109 | generator_.ConsumeData(stream2_id, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 1110 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1111 | |
| 1112 | // Ensure the packet is successfully created. |
| 1113 | ASSERT_EQ(1u, packets_.size()); |
| 1114 | ASSERT_TRUE(packets_[0].encrypted_buffer); |
| 1115 | ASSERT_EQ(1u, packets_[0].retransmittable_frames.size()); |
| 1116 | EXPECT_EQ(stream1_id, |
| 1117 | packets_[0].retransmittable_frames[0].stream_frame.stream_id); |
wub | 98669f5 | 2019-04-18 10:49:18 -0700 | [diff] [blame] | 1118 | |
| 1119 | // Since the second frame was not added, the packet's transmission type |
| 1120 | // should be the first frame's type. |
| 1121 | EXPECT_EQ(packets_[0].transmission_type, LOSS_RETRANSMISSION); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST_F(QuicPacketGeneratorTest, TestConnectionIdLength) { |
| 1125 | QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER); |
| 1126 | generator_.SetConnectionIdLength(0); |
| 1127 | EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, |
| 1128 | creator_->GetDestinationConnectionIdLength()); |
| 1129 | |
| 1130 | for (size_t i = 1; i < 10; i++) { |
| 1131 | generator_.SetConnectionIdLength(i); |
| 1132 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 1133 | EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, |
| 1134 | creator_->GetDestinationConnectionIdLength()); |
| 1135 | } else { |
| 1136 | EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, |
| 1137 | creator_->GetDestinationConnectionIdLength()); |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // Test whether SetMaxPacketLength() works in the situation when the queue is |
| 1143 | // empty, and we send three packets worth of data. |
| 1144 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_Initial) { |
| 1145 | delegate_.SetCanWriteAnything(); |
| 1146 | |
| 1147 | // Send enough data for three packets. |
| 1148 | size_t data_len = 3 * kDefaultMaxPacketSize + 1; |
| 1149 | size_t packet_len = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1150 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1151 | generator_.SetMaxPacketLength(packet_len); |
| 1152 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1153 | |
| 1154 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1155 | .Times(3) |
| 1156 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1157 | CreateData(data_len); |
| 1158 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1159 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1160 | iov_.iov_len, |
| 1161 | /*offset=*/0, FIN); |
| 1162 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1163 | EXPECT_TRUE(consumed.fin_consumed); |
| 1164 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1165 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1166 | |
| 1167 | // We expect three packets, and first two of them have to be of packet_len |
| 1168 | // size. We check multiple packets (instead of just one) because we want to |
| 1169 | // ensure that |max_packet_length_| does not get changed incorrectly by the |
| 1170 | // generator after first packet is serialized. |
| 1171 | ASSERT_EQ(3u, packets_.size()); |
| 1172 | EXPECT_EQ(packet_len, packets_[0].encrypted_length); |
| 1173 | EXPECT_EQ(packet_len, packets_[1].encrypted_length); |
| 1174 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1175 | } |
| 1176 | |
| 1177 | // Test whether SetMaxPacketLength() works in the situation when we first write |
| 1178 | // data, then change packet size, then write data again. |
| 1179 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_Middle) { |
| 1180 | delegate_.SetCanWriteAnything(); |
| 1181 | |
| 1182 | // We send enough data to overflow default packet length, but not the altered |
| 1183 | // one. |
| 1184 | size_t data_len = kDefaultMaxPacketSize; |
| 1185 | size_t packet_len = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1186 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1187 | |
| 1188 | // We expect to see three packets in total. |
| 1189 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1190 | .Times(3) |
| 1191 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1192 | |
| 1193 | // Send two packets before packet size change. |
| 1194 | CreateData(data_len); |
| 1195 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1196 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1197 | iov_.iov_len, |
| 1198 | /*offset=*/0, NO_FIN); |
| 1199 | generator_.Flush(); |
| 1200 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1201 | EXPECT_FALSE(consumed.fin_consumed); |
| 1202 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1203 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1204 | |
| 1205 | // Make sure we already have two packets. |
| 1206 | ASSERT_EQ(2u, packets_.size()); |
| 1207 | |
| 1208 | // Increase packet size. |
| 1209 | generator_.SetMaxPacketLength(packet_len); |
| 1210 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1211 | |
| 1212 | // Send a packet after packet size change. |
| 1213 | CreateData(data_len); |
| 1214 | generator_.AttachPacketFlusher(); |
| 1215 | consumed = generator_.ConsumeData( |
| 1216 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1217 | iov_.iov_len, data_len, FIN); |
| 1218 | generator_.Flush(); |
| 1219 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1220 | EXPECT_TRUE(consumed.fin_consumed); |
| 1221 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1222 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1223 | |
| 1224 | // We expect first data chunk to get fragmented, but the second one to fit |
| 1225 | // into a single packet. |
| 1226 | ASSERT_EQ(3u, packets_.size()); |
| 1227 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1228 | EXPECT_LE(kDefaultMaxPacketSize, packets_[2].encrypted_length); |
| 1229 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1230 | } |
| 1231 | |
| 1232 | // Test whether SetMaxPacketLength() works correctly when we force the change of |
| 1233 | // the packet size in the middle of the batched packet. |
| 1234 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) { |
| 1235 | delegate_.SetCanWriteAnything(); |
| 1236 | |
| 1237 | size_t first_write_len = kDefaultMaxPacketSize / 2; |
| 1238 | size_t packet_len = kDefaultMaxPacketSize + 100; |
| 1239 | size_t second_write_len = packet_len + 1; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1240 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1241 | |
| 1242 | // First send half of the packet worth of data. We are in the batch mode, so |
| 1243 | // should not cause packet serialization. |
| 1244 | CreateData(first_write_len); |
| 1245 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1246 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1247 | iov_.iov_len, |
| 1248 | /*offset=*/0, NO_FIN); |
| 1249 | EXPECT_EQ(first_write_len, consumed.bytes_consumed); |
| 1250 | EXPECT_FALSE(consumed.fin_consumed); |
| 1251 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1252 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1253 | |
| 1254 | // Make sure we have no packets so far. |
| 1255 | ASSERT_EQ(0u, packets_.size()); |
| 1256 | |
| 1257 | // Expect a packet to be flushed. |
| 1258 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1259 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1260 | |
| 1261 | // Increase packet size after flushing all frames. |
| 1262 | // Ensure it's immediately enacted. |
| 1263 | generator_.FlushAllQueuedFrames(); |
| 1264 | generator_.SetMaxPacketLength(packet_len); |
| 1265 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1266 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1267 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1268 | |
| 1269 | // We expect to see exactly one packet serialized after that, because we send |
| 1270 | // a value somewhat exceeding new max packet size, and the tail data does not |
| 1271 | // get serialized because we are still in the batch mode. |
| 1272 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1273 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1274 | |
| 1275 | // Send a more than a packet worth of data to the same stream. This should |
| 1276 | // trigger serialization of one packet, and queue another one. |
| 1277 | CreateData(second_write_len); |
| 1278 | consumed = generator_.ConsumeData( |
| 1279 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1280 | iov_.iov_len, |
| 1281 | /*offset=*/first_write_len, FIN); |
| 1282 | EXPECT_EQ(second_write_len, consumed.bytes_consumed); |
| 1283 | EXPECT_TRUE(consumed.fin_consumed); |
| 1284 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1285 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1286 | |
| 1287 | // We expect the first packet to be underfilled, and the second packet be up |
| 1288 | // to the new max packet size. |
| 1289 | ASSERT_EQ(2u, packets_.size()); |
| 1290 | EXPECT_GT(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1291 | EXPECT_EQ(packet_len, packets_[1].encrypted_length); |
| 1292 | |
| 1293 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1294 | } |
| 1295 | |
| 1296 | // Test sending a connectivity probing packet. |
| 1297 | TEST_F(QuicPacketGeneratorTest, GenerateConnectivityProbingPacket) { |
| 1298 | delegate_.SetCanWriteAnything(); |
| 1299 | |
| 1300 | OwningSerializedPacketPointer probing_packet; |
| 1301 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1302 | QuicPathFrameBuffer payload = { |
| 1303 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 1304 | probing_packet = |
| 1305 | generator_.SerializePathChallengeConnectivityProbingPacket(&payload); |
| 1306 | } else { |
| 1307 | probing_packet = generator_.SerializeConnectivityProbingPacket(); |
| 1308 | } |
| 1309 | |
| 1310 | ASSERT_TRUE(simple_framer_.ProcessPacket(QuicEncryptedPacket( |
| 1311 | probing_packet->encrypted_buffer, probing_packet->encrypted_length))); |
| 1312 | |
| 1313 | EXPECT_EQ(2u, simple_framer_.num_frames()); |
| 1314 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1315 | EXPECT_EQ(1u, simple_framer_.path_challenge_frames().size()); |
| 1316 | } else { |
| 1317 | EXPECT_EQ(1u, simple_framer_.ping_frames().size()); |
| 1318 | } |
| 1319 | EXPECT_EQ(1u, simple_framer_.padding_frames().size()); |
| 1320 | } |
| 1321 | |
| 1322 | // Test sending an MTU probe, without any surrounding data. |
| 1323 | TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_Simple) { |
| 1324 | delegate_.SetCanWriteAnything(); |
| 1325 | |
| 1326 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1327 | static_assert(target_mtu < kMaxOutgoingPacketSize, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1328 | "The MTU probe used by the test exceeds maximum packet size"); |
| 1329 | |
| 1330 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1331 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1332 | |
| 1333 | generator_.GenerateMtuDiscoveryPacket(target_mtu); |
| 1334 | |
| 1335 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1336 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1337 | ASSERT_EQ(1u, packets_.size()); |
| 1338 | EXPECT_EQ(target_mtu, packets_[0].encrypted_length); |
| 1339 | |
| 1340 | PacketContents contents; |
| 1341 | contents.num_mtu_discovery_frames = 1; |
| 1342 | contents.num_padding_frames = 1; |
| 1343 | CheckPacketContains(contents, 0); |
| 1344 | } |
| 1345 | |
| 1346 | // Test sending an MTU probe. Surround it with data, to ensure that it resets |
| 1347 | // the MTU to the value before the probe was sent. |
| 1348 | TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_SurroundedByData) { |
| 1349 | delegate_.SetCanWriteAnything(); |
| 1350 | |
| 1351 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1352 | static_assert(target_mtu < kMaxOutgoingPacketSize, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1353 | "The MTU probe used by the test exceeds maximum packet size"); |
| 1354 | |
| 1355 | // Send enough data so it would always cause two packets to be sent. |
| 1356 | const size_t data_len = target_mtu + 1; |
| 1357 | |
| 1358 | // Send a total of five packets: two packets before the probe, the probe |
| 1359 | // itself, and two packets after the probe. |
| 1360 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1361 | .Times(5) |
| 1362 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1363 | |
| 1364 | // Send data before the MTU probe. |
| 1365 | CreateData(data_len); |
| 1366 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1367 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1368 | iov_.iov_len, |
| 1369 | /*offset=*/0, NO_FIN); |
| 1370 | generator_.Flush(); |
| 1371 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1372 | EXPECT_FALSE(consumed.fin_consumed); |
| 1373 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1374 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1375 | |
| 1376 | // Send the MTU probe. |
| 1377 | generator_.GenerateMtuDiscoveryPacket(target_mtu); |
| 1378 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1379 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1380 | |
| 1381 | // Send data after the MTU probe. |
| 1382 | CreateData(data_len); |
| 1383 | generator_.AttachPacketFlusher(); |
| 1384 | consumed = generator_.ConsumeData( |
| 1385 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1386 | iov_.iov_len, |
| 1387 | /*offset=*/data_len, FIN); |
| 1388 | generator_.Flush(); |
| 1389 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1390 | EXPECT_TRUE(consumed.fin_consumed); |
| 1391 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1392 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1393 | |
| 1394 | ASSERT_EQ(5u, packets_.size()); |
| 1395 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1396 | EXPECT_EQ(target_mtu, packets_[2].encrypted_length); |
| 1397 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[3].encrypted_length); |
| 1398 | |
| 1399 | PacketContents probe_contents; |
| 1400 | probe_contents.num_mtu_discovery_frames = 1; |
| 1401 | probe_contents.num_padding_frames = 1; |
| 1402 | |
| 1403 | CheckPacketHasSingleStreamFrame(0); |
| 1404 | CheckPacketHasSingleStreamFrame(1); |
| 1405 | CheckPacketContains(probe_contents, 2); |
| 1406 | CheckPacketHasSingleStreamFrame(3); |
| 1407 | CheckPacketHasSingleStreamFrame(4); |
| 1408 | } |
| 1409 | |
| 1410 | TEST_F(QuicPacketGeneratorTest, DontCrashOnInvalidStopWaiting) { |
| 1411 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 1412 | return; |
| 1413 | } |
| 1414 | // Test added to ensure the generator does not crash when an invalid frame is |
| 1415 | // added. Because this is an indication of internal programming errors, |
| 1416 | // DFATALs are expected. |
| 1417 | // A 1 byte packet number length can't encode a gap of 1000. |
| 1418 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1000); |
| 1419 | |
| 1420 | delegate_.SetCanNotWrite(); |
| 1421 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1422 | generator_.SetShouldSendAck(true); |
| 1423 | } |
| 1424 | delegate_.SetCanWriteAnything(); |
| 1425 | |
| 1426 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1427 | // Set up frames to write into the creator when control frames are written. |
| 1428 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 1429 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 1430 | EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_)); |
| 1431 | // Generator should have queued control frames, and creator should be empty. |
| 1432 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1433 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1434 | EXPECT_FALSE(creator_->HasPendingFrames()); |
| 1435 | } |
| 1436 | |
| 1437 | // This will not serialize any packets, because of the invalid frame. |
| 1438 | EXPECT_CALL(delegate_, |
| 1439 | OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, _, |
| 1440 | ConnectionCloseSource::FROM_SELF)); |
| 1441 | EXPECT_QUIC_BUG(generator_.Flush(), |
| 1442 | "packet_number_length 1 is too small " |
| 1443 | "for least_unacked_delta: 1001"); |
| 1444 | } |
| 1445 | |
| 1446 | // Regression test for b/31486443. |
| 1447 | TEST_F(QuicPacketGeneratorTest, ConnectionCloseFrameLargerThanPacketSize) { |
| 1448 | delegate_.SetCanWriteAnything(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1449 | char buf[2000] = {}; |
| 1450 | QuicStringPiece error_details(buf, 2000); |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 1451 | QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame( |
| 1452 | QUIC_PACKET_WRITE_ERROR, std::string(error_details)); |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 1453 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1454 | frame->close_type = IETF_QUIC_TRANSPORT_CONNECTION_CLOSE; |
| 1455 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1456 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(frame), |
| 1457 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1458 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1459 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1460 | } |
| 1461 | |
| 1462 | TEST_F(QuicPacketGeneratorTest, RandomPaddingAfterFinSingleStreamSinglePacket) { |
| 1463 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1464 | char buf[kStreamFramePayloadSize] = {}; |
| 1465 | const QuicStreamId kDataStreamId = 5; |
| 1466 | // Set the packet size be enough for one stream frame with 0 stream offset and |
| 1467 | // max size of random padding. |
| 1468 | size_t length = |
| 1469 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1470 | GetPacketHeaderSize( |
| 1471 | framer_.transport_version(), |
| 1472 | creator_->GetDestinationConnectionIdLength(), |
| 1473 | creator_->GetSourceConnectionIdLength(), |
| 1474 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1475 | !kIncludeDiversificationNonce, |
| 1476 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1477 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1478 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1479 | QuicFramer::GetMinStreamFrameSize( |
| 1480 | framer_.transport_version(), kDataStreamId, 0, |
| 1481 | /*last_frame_in_packet=*/false, |
| 1482 | kStreamFramePayloadSize + kMaxNumRandomPaddingBytes) + |
| 1483 | kStreamFramePayloadSize + kMaxNumRandomPaddingBytes; |
| 1484 | generator_.SetMaxPacketLength(length); |
| 1485 | delegate_.SetCanWriteAnything(); |
| 1486 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1487 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1488 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1489 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1490 | kDataStreamId, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1491 | generator_.Flush(); |
| 1492 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1493 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1494 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1495 | |
| 1496 | EXPECT_EQ(1u, packets_.size()); |
| 1497 | PacketContents contents; |
| 1498 | // The packet has both stream and padding frames. |
| 1499 | contents.num_padding_frames = 1; |
| 1500 | contents.num_stream_frames = 1; |
| 1501 | CheckPacketContains(contents, 0); |
| 1502 | } |
| 1503 | |
| 1504 | TEST_F(QuicPacketGeneratorTest, |
| 1505 | RandomPaddingAfterFinSingleStreamMultiplePackets) { |
| 1506 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1507 | char buf[kStreamFramePayloadSize] = {}; |
| 1508 | const QuicStreamId kDataStreamId = 5; |
| 1509 | // Set the packet size be enough for one stream frame with 0 stream offset + |
| 1510 | // 1. One or more packets will accommodate. |
| 1511 | size_t length = |
| 1512 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1513 | GetPacketHeaderSize( |
| 1514 | framer_.transport_version(), |
| 1515 | creator_->GetDestinationConnectionIdLength(), |
| 1516 | creator_->GetSourceConnectionIdLength(), |
| 1517 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1518 | !kIncludeDiversificationNonce, |
| 1519 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1520 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1521 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1522 | QuicFramer::GetMinStreamFrameSize( |
| 1523 | framer_.transport_version(), kDataStreamId, 0, |
| 1524 | /*last_frame_in_packet=*/false, kStreamFramePayloadSize + 1) + |
| 1525 | kStreamFramePayloadSize + 1; |
| 1526 | generator_.SetMaxPacketLength(length); |
| 1527 | delegate_.SetCanWriteAnything(); |
| 1528 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1529 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1530 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1531 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1532 | kDataStreamId, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1533 | generator_.Flush(); |
| 1534 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1535 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1536 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1537 | |
| 1538 | EXPECT_LE(1u, packets_.size()); |
| 1539 | PacketContents contents; |
| 1540 | // The first packet has both stream and padding frames. |
| 1541 | contents.num_stream_frames = 1; |
| 1542 | contents.num_padding_frames = 1; |
| 1543 | CheckPacketContains(contents, 0); |
| 1544 | |
| 1545 | for (size_t i = 1; i < packets_.size(); ++i) { |
| 1546 | // Following packets only have paddings. |
| 1547 | contents.num_stream_frames = 0; |
| 1548 | contents.num_padding_frames = 1; |
| 1549 | CheckPacketContains(contents, i); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | TEST_F(QuicPacketGeneratorTest, |
| 1554 | RandomPaddingAfterFinMultipleStreamsMultiplePackets) { |
| 1555 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1556 | char buf[kStreamFramePayloadSize] = {}; |
| 1557 | const QuicStreamId kDataStreamId1 = 5; |
| 1558 | const QuicStreamId kDataStreamId2 = 6; |
| 1559 | // Set the packet size be enough for first frame with 0 stream offset + second |
| 1560 | // frame + 1 byte payload. two or more packets will accommodate. |
| 1561 | size_t length = |
| 1562 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1563 | GetPacketHeaderSize( |
| 1564 | framer_.transport_version(), |
| 1565 | creator_->GetDestinationConnectionIdLength(), |
| 1566 | creator_->GetSourceConnectionIdLength(), |
| 1567 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1568 | !kIncludeDiversificationNonce, |
| 1569 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1570 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1571 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1572 | QuicFramer::GetMinStreamFrameSize( |
| 1573 | framer_.transport_version(), kDataStreamId1, 0, |
| 1574 | /*last_frame_in_packet=*/false, kStreamFramePayloadSize) + |
| 1575 | kStreamFramePayloadSize + |
| 1576 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), |
| 1577 | kDataStreamId1, 0, |
| 1578 | /*last_frame_in_packet=*/false, 1) + |
| 1579 | 1; |
| 1580 | generator_.SetMaxPacketLength(length); |
| 1581 | delegate_.SetCanWriteAnything(); |
| 1582 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1583 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1584 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1585 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1586 | kDataStreamId1, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1587 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1588 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1589 | consumed = generator_.ConsumeData(kDataStreamId2, &iov_, 1u, iov_.iov_len, 0, |
| 1590 | FIN_AND_PADDING); |
| 1591 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1592 | generator_.Flush(); |
| 1593 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1594 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1595 | |
| 1596 | EXPECT_LE(2u, packets_.size()); |
| 1597 | PacketContents contents; |
| 1598 | // The first packet has two stream frames. |
| 1599 | contents.num_stream_frames = 2; |
| 1600 | CheckPacketContains(contents, 0); |
| 1601 | |
| 1602 | // The second packet has one stream frame and padding frames. |
| 1603 | contents.num_stream_frames = 1; |
| 1604 | contents.num_padding_frames = 1; |
| 1605 | CheckPacketContains(contents, 1); |
| 1606 | |
| 1607 | for (size_t i = 2; i < packets_.size(); ++i) { |
| 1608 | // Following packets only have paddings. |
| 1609 | contents.num_stream_frames = 0; |
| 1610 | contents.num_padding_frames = 1; |
| 1611 | CheckPacketContains(contents, i); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | TEST_F(QuicPacketGeneratorTest, AddMessageFrame) { |
| 1616 | if (framer_.transport_version() <= QUIC_VERSION_44) { |
| 1617 | return; |
| 1618 | } |
| 1619 | quic::QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 1620 | delegate_.SetCanWriteAnything(); |
| 1621 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1622 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1623 | |
| 1624 | MakeIOVector("foo", &iov_); |
| 1625 | generator_.ConsumeData( |
| 1626 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1627 | iov_.iov_len, 0, FIN); |
| 1628 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, |
| 1629 | generator_.AddMessageFrame( |
| 1630 | 1, MakeSpan(&allocator_, "message", &storage))); |
| 1631 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1632 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1633 | |
| 1634 | // Add a message which causes the flush of current packet. |
| 1635 | EXPECT_EQ( |
| 1636 | MESSAGE_STATUS_SUCCESS, |
| 1637 | generator_.AddMessageFrame( |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1638 | 2, MakeSpan( |
| 1639 | &allocator_, |
| 1640 | std::string(generator_.GetCurrentLargestMessagePayload(), 'a'), |
| 1641 | &storage))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1642 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1643 | |
| 1644 | // Failed to send messages which cannot fit into one packet. |
| 1645 | EXPECT_EQ( |
| 1646 | MESSAGE_STATUS_TOO_LARGE, |
| 1647 | generator_.AddMessageFrame( |
| 1648 | 3, |
| 1649 | MakeSpan(&allocator_, |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1650 | std::string( |
| 1651 | generator_.GetCurrentLargestMessagePayload() + 10, 'a'), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1652 | &storage))); |
| 1653 | } |
| 1654 | |
| 1655 | } // namespace test |
| 1656 | } // namespace quic |