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 | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 590 | std::string data = "foo bar"; |
| 591 | MakeIOVector(data, &iov_); |
| 592 | size_t consumed_bytes = 0; |
| 593 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 594 | consumed_bytes = generator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data, 0); |
| 595 | } else { |
| 596 | consumed_bytes = |
| 597 | generator_ |
| 598 | .ConsumeData( |
| 599 | QuicUtils::GetCryptoStreamId(framer_.transport_version()), |
| 600 | &iov_, 1u, iov_.iov_len, 0, NO_FIN) |
| 601 | .bytes_consumed; |
| 602 | } |
| 603 | EXPECT_EQ(7u, consumed_bytes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 604 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 605 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 606 | |
| 607 | PacketContents contents; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 608 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 609 | contents.num_crypto_frames = 1; |
| 610 | } else { |
| 611 | contents.num_stream_frames = 1; |
| 612 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 613 | contents.num_padding_frames = 1; |
| 614 | CheckPacketContains(contents, 0); |
| 615 | |
| 616 | ASSERT_EQ(1u, packets_.size()); |
| 617 | ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength()); |
| 618 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 619 | } |
| 620 | |
| 621 | // Test the behavior of ConsumeData when the data is for the crypto handshake |
| 622 | // stream, but padding is disabled. |
| 623 | TEST_F(QuicPacketGeneratorTest, ConsumeData_Handshake_PaddingDisabled) { |
| 624 | generator_.set_fully_pad_crypto_hadshake_packets(false); |
| 625 | |
| 626 | delegate_.SetCanWriteAnything(); |
| 627 | |
| 628 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 629 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 630 | std::string data = "foo"; |
| 631 | MakeIOVector(data, &iov_); |
| 632 | size_t bytes_consumed = 0; |
| 633 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 634 | bytes_consumed = generator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data, 0); |
| 635 | } else { |
| 636 | bytes_consumed = |
| 637 | generator_ |
| 638 | .ConsumeData( |
| 639 | QuicUtils::GetCryptoStreamId(framer_.transport_version()), |
| 640 | &iov_, 1u, iov_.iov_len, 0, NO_FIN) |
| 641 | .bytes_consumed; |
| 642 | } |
| 643 | EXPECT_EQ(3u, bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 644 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 645 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 646 | |
| 647 | PacketContents contents; |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 648 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 649 | contents.num_crypto_frames = 1; |
| 650 | } else { |
| 651 | contents.num_stream_frames = 1; |
| 652 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 653 | contents.num_padding_frames = 0; |
| 654 | CheckPacketContains(contents, 0); |
| 655 | |
| 656 | ASSERT_EQ(1u, packets_.size()); |
| 657 | |
| 658 | // Packet is not fully padded, but we want to future packets to be larger. |
| 659 | ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength()); |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 660 | size_t expected_packet_length = 27; |
| 661 | if (QuicVersionUsesCryptoFrames(framer_.transport_version())) { |
| 662 | // The framing of CRYPTO frames is slightly different than that of stream |
| 663 | // frames, so the expected packet length differs slightly. |
| 664 | expected_packet_length = 28; |
| 665 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 666 | if (framer_.version().HasHeaderProtection()) { |
| 667 | expected_packet_length = 29; |
| 668 | } |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 669 | EXPECT_EQ(expected_packet_length, packets_[0].encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) { |
| 673 | delegate_.SetCanWriteAnything(); |
| 674 | |
| 675 | EXPECT_QUIC_BUG(generator_.ConsumeData(QuicUtils::GetHeadersStreamId( |
| 676 | framer_.transport_version()), |
| 677 | nullptr, 0, 0, 0, NO_FIN), |
| 678 | "Attempt to consume empty data without FIN."); |
| 679 | } |
| 680 | |
| 681 | TEST_F(QuicPacketGeneratorTest, |
| 682 | ConsumeDataMultipleTimes_WritableAndShouldNotFlush) { |
| 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 = |
| 691 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 3, NO_FIN); |
| 692 | EXPECT_EQ(4u, consumed.bytes_consumed); |
| 693 | EXPECT_FALSE(consumed.fin_consumed); |
| 694 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 695 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 696 | } |
| 697 | |
| 698 | TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) { |
| 699 | delegate_.SetCanWriteAnything(); |
| 700 | |
| 701 | MakeIOVector("foo", &iov_); |
| 702 | generator_.ConsumeData( |
| 703 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 704 | iov_.iov_len, 0, FIN); |
| 705 | MakeIOVector("quux", &iov_); |
| 706 | QuicConsumedData consumed = generator_.ConsumeData( |
| 707 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 708 | iov_.iov_len, 3, NO_FIN); |
| 709 | EXPECT_EQ(4u, consumed.bytes_consumed); |
| 710 | EXPECT_FALSE(consumed.fin_consumed); |
| 711 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 712 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 713 | |
| 714 | // Now both frames will be flushed out. |
| 715 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 716 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 717 | generator_.Flush(); |
| 718 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 719 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 720 | |
| 721 | PacketContents contents; |
| 722 | contents.num_stream_frames = 2; |
| 723 | CheckPacketContains(contents, 0); |
| 724 | } |
| 725 | |
| 726 | TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) { |
| 727 | // Set the packet size be enough for two stream frames with 0 stream offset, |
| 728 | // but not enough for a stream frame of 0 offset and one with non-zero offset. |
| 729 | size_t length = |
| 730 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 731 | GetPacketHeaderSize( |
| 732 | framer_.transport_version(), |
| 733 | creator_->GetDestinationConnectionIdLength(), |
| 734 | creator_->GetSourceConnectionIdLength(), |
| 735 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 736 | !kIncludeDiversificationNonce, |
| 737 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 738 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 739 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 740 | // Add an extra 3 bytes for the payload and 1 byte so |
| 741 | // BytesFree is larger than the GetMinStreamFrameSize. |
| 742 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), 1, 0, |
| 743 | false, 3) + |
| 744 | 3 + |
| 745 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), 1, 0, true, |
| 746 | 1) + |
| 747 | 1; |
| 748 | generator_.SetMaxPacketLength(length); |
| 749 | delegate_.SetCanWriteAnything(); |
| 750 | { |
| 751 | InSequence dummy; |
| 752 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 753 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 754 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 755 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 756 | } |
| 757 | // Queue enough data to prevent a stream frame with a non-zero offset from |
| 758 | // fitting. |
| 759 | MakeIOVector("foo", &iov_); |
| 760 | QuicConsumedData consumed = generator_.ConsumeData( |
| 761 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 762 | iov_.iov_len, 0, NO_FIN); |
| 763 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 764 | EXPECT_FALSE(consumed.fin_consumed); |
| 765 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 766 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 767 | |
| 768 | // This frame will not fit with the existing frame, causing the queued frame |
| 769 | // to be serialized, and it will be added to a new open packet. |
| 770 | MakeIOVector("bar", &iov_); |
| 771 | consumed = generator_.ConsumeData( |
| 772 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 773 | iov_.iov_len, 3, FIN); |
| 774 | EXPECT_EQ(3u, consumed.bytes_consumed); |
| 775 | EXPECT_TRUE(consumed.fin_consumed); |
| 776 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 777 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 778 | |
| 779 | creator_->Flush(); |
| 780 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 781 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 782 | |
| 783 | PacketContents contents; |
| 784 | contents.num_stream_frames = 1; |
| 785 | CheckPacketContains(contents, 0); |
| 786 | CheckPacketContains(contents, 1); |
| 787 | } |
| 788 | |
| 789 | TEST_F(QuicPacketGeneratorTest, ConsumeDataFastPath) { |
| 790 | delegate_.SetCanWriteAnything(); |
| 791 | generator_.SetCanSetTransmissionType(true); |
| 792 | generator_.SetTransmissionType(LOSS_RETRANSMISSION); |
| 793 | |
| 794 | // Create a 10000 byte IOVector. |
| 795 | CreateData(10000); |
| 796 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 797 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 798 | QuicConsumedData consumed = generator_.ConsumeDataFastPath( |
| 799 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 800 | iov_.iov_len, 0, true); |
| 801 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 802 | EXPECT_TRUE(consumed.fin_consumed); |
| 803 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 804 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 805 | |
| 806 | PacketContents contents; |
| 807 | contents.num_stream_frames = 1; |
| 808 | CheckPacketContains(contents, 0); |
| 809 | EXPECT_FALSE(packets_.empty()); |
| 810 | SerializedPacket packet = packets_.back(); |
| 811 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 812 | EXPECT_EQ(LOSS_RETRANSMISSION, packet.transmission_type); |
| 813 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 814 | const QuicStreamFrame& stream_frame = |
| 815 | packet.retransmittable_frames.front().stream_frame; |
| 816 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 817 | } |
| 818 | |
| 819 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLarge) { |
| 820 | delegate_.SetCanWriteAnything(); |
| 821 | |
| 822 | // Create a 10000 byte IOVector. |
| 823 | CreateData(10000); |
| 824 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 825 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 826 | QuicConsumedData consumed = generator_.ConsumeData( |
| 827 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 828 | iov_.iov_len, 0, FIN); |
| 829 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 830 | EXPECT_TRUE(consumed.fin_consumed); |
| 831 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 832 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 833 | |
| 834 | PacketContents contents; |
| 835 | contents.num_stream_frames = 1; |
| 836 | CheckPacketContains(contents, 0); |
| 837 | EXPECT_FALSE(packets_.empty()); |
| 838 | SerializedPacket packet = packets_.back(); |
| 839 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 840 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 841 | const QuicStreamFrame& stream_frame = |
| 842 | packet.retransmittable_frames.front().stream_frame; |
| 843 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 844 | } |
| 845 | |
| 846 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLargeSendAckFalse) { |
| 847 | delegate_.SetCanNotWrite(); |
| 848 | |
| 849 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 850 | generator_.SetShouldSendAck(false); |
| 851 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 852 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 853 | const bool success = |
| 854 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 855 | /*bundle_ack=*/true); |
| 856 | if (generator_.deprecate_queued_control_frames()) { |
| 857 | EXPECT_FALSE(success); |
| 858 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 859 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 860 | } else { |
| 861 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 862 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 863 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 864 | |
| 865 | delegate_.SetCanWriteAnything(); |
| 866 | |
| 867 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 868 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 869 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 870 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 871 | if (generator_.deprecate_queued_control_frames()) { |
| 872 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 873 | /*bundle_ack=*/false); |
| 874 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 875 | |
| 876 | // Create a 10000 byte IOVector. |
| 877 | CreateData(10000); |
| 878 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 879 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 880 | if (generator_.deprecate_queued_control_frames()) { |
| 881 | generator_.ConsumeRetransmittableControlFrame( |
| 882 | QuicFrame(CreateRstStreamFrame()), |
| 883 | /*bundle_ack=*/true); |
| 884 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 885 | QuicConsumedData consumed = generator_.ConsumeData( |
| 886 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 887 | iov_.iov_len, 0, FIN); |
| 888 | generator_.Flush(); |
| 889 | |
| 890 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 891 | EXPECT_TRUE(consumed.fin_consumed); |
| 892 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 893 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 894 | |
| 895 | EXPECT_FALSE(packets_.empty()); |
| 896 | SerializedPacket packet = packets_.back(); |
| 897 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 898 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 899 | const QuicStreamFrame& stream_frame = |
| 900 | packet.retransmittable_frames.front().stream_frame; |
| 901 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 902 | } |
| 903 | |
| 904 | TEST_F(QuicPacketGeneratorTest, ConsumeDataLargeSendAckTrue) { |
| 905 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 906 | return; |
| 907 | } |
| 908 | delegate_.SetCanNotWrite(); |
| 909 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 910 | generator_.SetShouldSendAck(true /* stop_waiting */); |
| 911 | } |
| 912 | delegate_.SetCanWriteAnything(); |
| 913 | |
| 914 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 915 | // Set up frames to write into the creator when control frames are written. |
| 916 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 917 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 918 | EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_)); |
| 919 | // Generator should have queued control frames, and creator should be empty. |
| 920 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 921 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 922 | EXPECT_FALSE(creator_->HasPendingFrames()); |
| 923 | } |
| 924 | |
| 925 | // Create a 10000 byte IOVector. |
| 926 | CreateData(10000); |
| 927 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 928 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 929 | QuicConsumedData consumed = generator_.ConsumeData( |
| 930 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 931 | iov_.iov_len, 0, FIN); |
| 932 | generator_.Flush(); |
| 933 | |
| 934 | EXPECT_EQ(10000u, consumed.bytes_consumed); |
| 935 | EXPECT_TRUE(consumed.fin_consumed); |
| 936 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 937 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 938 | |
| 939 | EXPECT_FALSE(packets_.empty()); |
| 940 | SerializedPacket packet = packets_.back(); |
| 941 | EXPECT_TRUE(!packet.retransmittable_frames.empty()); |
| 942 | EXPECT_EQ(STREAM_FRAME, packet.retransmittable_frames.front().type); |
| 943 | const QuicStreamFrame& stream_frame = |
| 944 | packet.retransmittable_frames.front().stream_frame; |
| 945 | EXPECT_EQ(10000u, stream_frame.data_length + stream_frame.offset); |
| 946 | } |
| 947 | |
| 948 | TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) { |
| 949 | delegate_.SetCanNotWrite(); |
| 950 | |
| 951 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 952 | generator_.SetShouldSendAck(false); |
| 953 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 954 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 955 | const bool consumed = |
| 956 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 957 | /*bundle_ack=*/true); |
| 958 | if (generator_.deprecate_queued_control_frames()) { |
| 959 | EXPECT_FALSE(consumed); |
| 960 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 961 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 962 | } else { |
| 963 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 964 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 965 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 966 | EXPECT_FALSE(generator_.HasPendingStreamFramesOfStream(3)); |
| 967 | |
| 968 | delegate_.SetCanWriteAnything(); |
| 969 | |
| 970 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 971 | // When the first write operation is invoked, the ack frame will be |
| 972 | // returned. |
| 973 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 974 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 975 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 976 | if (generator_.deprecate_queued_control_frames()) { |
| 977 | EXPECT_TRUE( |
| 978 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 979 | /*bundle_ack=*/false)); |
| 980 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 981 | // Send some data and a control frame |
| 982 | MakeIOVector("quux", &iov_); |
| 983 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 984 | if (framer_.transport_version() != QUIC_VERSION_99) { |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 985 | generator_.ConsumeRetransmittableControlFrame( |
| 986 | QuicFrame(CreateGoAwayFrame()), |
| 987 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 988 | } |
| 989 | EXPECT_TRUE(generator_.HasPendingStreamFramesOfStream(3)); |
| 990 | |
| 991 | // All five frames will be flushed out in a single packet. |
| 992 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 993 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 994 | generator_.Flush(); |
| 995 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 996 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 997 | EXPECT_FALSE(generator_.HasPendingStreamFramesOfStream(3)); |
| 998 | |
| 999 | PacketContents contents; |
| 1000 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1001 | // ACK will be flushed by connection. |
| 1002 | contents.num_ack_frames = 0; |
| 1003 | } else { |
| 1004 | contents.num_ack_frames = 1; |
| 1005 | } |
| 1006 | if (framer_.transport_version() != QUIC_VERSION_99) { |
| 1007 | contents.num_goaway_frames = 1; |
| 1008 | } else { |
| 1009 | contents.num_goaway_frames = 0; |
| 1010 | } |
| 1011 | contents.num_rst_stream_frames = 1; |
| 1012 | contents.num_stream_frames = 1; |
| 1013 | CheckPacketContains(contents, 0); |
| 1014 | } |
| 1015 | |
| 1016 | TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) { |
| 1017 | delegate_.SetCanNotWrite(); |
| 1018 | |
| 1019 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1020 | generator_.SetShouldSendAck(false); |
| 1021 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1022 | QuicRstStreamFrame* rst_frame = CreateRstStreamFrame(); |
| 1023 | const bool success = |
| 1024 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 1025 | /*bundle_ack=*/true); |
| 1026 | if (generator_.deprecate_queued_control_frames()) { |
| 1027 | EXPECT_FALSE(success); |
| 1028 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1029 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1030 | } else { |
| 1031 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1032 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1033 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1034 | |
| 1035 | delegate_.SetCanWriteAnything(); |
| 1036 | |
| 1037 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1038 | // When the first write operation is invoked, the ack frame will be |
| 1039 | // returned. |
| 1040 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 1041 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 1042 | } |
| 1043 | |
| 1044 | { |
| 1045 | InSequence dummy; |
| 1046 | // All five frames will be flushed out in a single packet |
| 1047 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1048 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1049 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1050 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1051 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1052 | if (generator_.deprecate_queued_control_frames()) { |
| 1053 | EXPECT_TRUE( |
| 1054 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(rst_frame), |
| 1055 | /*bundle_ack=*/false)); |
| 1056 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1057 | // Send enough data to exceed one packet |
| 1058 | size_t data_len = kDefaultMaxPacketSize + 100; |
| 1059 | CreateData(data_len); |
| 1060 | QuicConsumedData consumed = |
| 1061 | generator_.ConsumeData(3, &iov_, 1u, iov_.iov_len, 0, FIN); |
| 1062 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1063 | EXPECT_TRUE(consumed.fin_consumed); |
| 1064 | if (framer_.transport_version() != QUIC_VERSION_99) { |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1065 | generator_.ConsumeRetransmittableControlFrame( |
| 1066 | QuicFrame(CreateGoAwayFrame()), |
| 1067 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | generator_.Flush(); |
| 1071 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1072 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1073 | |
| 1074 | // The first packet should have the queued data and part of the stream data. |
| 1075 | PacketContents contents; |
| 1076 | if (GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1077 | // ACK will be sent by connection. |
| 1078 | contents.num_ack_frames = 0; |
| 1079 | } else { |
| 1080 | contents.num_ack_frames = 1; |
| 1081 | } |
| 1082 | contents.num_rst_stream_frames = 1; |
| 1083 | contents.num_stream_frames = 1; |
| 1084 | CheckPacketContains(contents, 0); |
| 1085 | |
| 1086 | // The second should have the remainder of the stream data. |
| 1087 | PacketContents contents2; |
| 1088 | if (framer_.transport_version() != QUIC_VERSION_99) { |
| 1089 | contents2.num_goaway_frames = 1; |
| 1090 | } else { |
| 1091 | contents2.num_goaway_frames = 0; |
| 1092 | } |
| 1093 | contents2.num_stream_frames = 1; |
| 1094 | CheckPacketContains(contents2, 1); |
| 1095 | } |
| 1096 | |
| 1097 | // Regression test of b/120493795. |
| 1098 | TEST_F(QuicPacketGeneratorTest, PacketTransmissionType) { |
| 1099 | delegate_.SetCanWriteAnything(); |
| 1100 | generator_.SetCanSetTransmissionType(true); |
| 1101 | |
| 1102 | // The first ConsumeData will fill the packet without flush. |
| 1103 | generator_.SetTransmissionType(LOSS_RETRANSMISSION); |
| 1104 | |
| 1105 | size_t data_len = 1324; |
| 1106 | CreateData(data_len); |
| 1107 | QuicStreamId stream1_id = |
| 1108 | QuicUtils::GetHeadersStreamId(framer_.transport_version()); |
| 1109 | QuicConsumedData consumed = |
| 1110 | generator_.ConsumeData(stream1_id, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 1111 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1112 | ASSERT_EQ(0u, creator_->BytesFree()) |
| 1113 | << "Test setup failed: Please increase data_len to " |
| 1114 | << data_len + creator_->BytesFree() << " bytes."; |
| 1115 | |
| 1116 | // The second ConsumeData can not be added to the packet and will flush. |
| 1117 | generator_.SetTransmissionType(NOT_RETRANSMISSION); |
| 1118 | |
| 1119 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1120 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1121 | |
| 1122 | QuicStreamId stream2_id = stream1_id + 4; |
| 1123 | |
| 1124 | consumed = |
| 1125 | generator_.ConsumeData(stream2_id, &iov_, 1u, iov_.iov_len, 0, NO_FIN); |
| 1126 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1127 | |
| 1128 | // Ensure the packet is successfully created. |
| 1129 | ASSERT_EQ(1u, packets_.size()); |
| 1130 | ASSERT_TRUE(packets_[0].encrypted_buffer); |
| 1131 | ASSERT_EQ(1u, packets_[0].retransmittable_frames.size()); |
| 1132 | EXPECT_EQ(stream1_id, |
| 1133 | packets_[0].retransmittable_frames[0].stream_frame.stream_id); |
wub | 98669f5 | 2019-04-18 10:49:18 -0700 | [diff] [blame] | 1134 | |
| 1135 | // Since the second frame was not added, the packet's transmission type |
| 1136 | // should be the first frame's type. |
| 1137 | EXPECT_EQ(packets_[0].transmission_type, LOSS_RETRANSMISSION); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | TEST_F(QuicPacketGeneratorTest, TestConnectionIdLength) { |
| 1141 | QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame^] | 1142 | generator_.SetServerConnectionIdLength(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1143 | EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, |
| 1144 | creator_->GetDestinationConnectionIdLength()); |
| 1145 | |
| 1146 | for (size_t i = 1; i < 10; i++) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame^] | 1147 | generator_.SetServerConnectionIdLength(i); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1148 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 1149 | EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, |
| 1150 | creator_->GetDestinationConnectionIdLength()); |
| 1151 | } else { |
| 1152 | EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, |
| 1153 | creator_->GetDestinationConnectionIdLength()); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | // Test whether SetMaxPacketLength() works in the situation when the queue is |
| 1159 | // empty, and we send three packets worth of data. |
| 1160 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_Initial) { |
| 1161 | delegate_.SetCanWriteAnything(); |
| 1162 | |
| 1163 | // Send enough data for three packets. |
| 1164 | size_t data_len = 3 * kDefaultMaxPacketSize + 1; |
| 1165 | size_t packet_len = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1166 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1167 | generator_.SetMaxPacketLength(packet_len); |
| 1168 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1169 | |
| 1170 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1171 | .Times(3) |
| 1172 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1173 | CreateData(data_len); |
| 1174 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1175 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1176 | iov_.iov_len, |
| 1177 | /*offset=*/0, FIN); |
| 1178 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1179 | EXPECT_TRUE(consumed.fin_consumed); |
| 1180 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1181 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1182 | |
| 1183 | // We expect three packets, and first two of them have to be of packet_len |
| 1184 | // size. We check multiple packets (instead of just one) because we want to |
| 1185 | // ensure that |max_packet_length_| does not get changed incorrectly by the |
| 1186 | // generator after first packet is serialized. |
| 1187 | ASSERT_EQ(3u, packets_.size()); |
| 1188 | EXPECT_EQ(packet_len, packets_[0].encrypted_length); |
| 1189 | EXPECT_EQ(packet_len, packets_[1].encrypted_length); |
| 1190 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1191 | } |
| 1192 | |
| 1193 | // Test whether SetMaxPacketLength() works in the situation when we first write |
| 1194 | // data, then change packet size, then write data again. |
| 1195 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_Middle) { |
| 1196 | delegate_.SetCanWriteAnything(); |
| 1197 | |
| 1198 | // We send enough data to overflow default packet length, but not the altered |
| 1199 | // one. |
| 1200 | size_t data_len = kDefaultMaxPacketSize; |
| 1201 | size_t packet_len = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1202 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1203 | |
| 1204 | // We expect to see three packets in total. |
| 1205 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1206 | .Times(3) |
| 1207 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1208 | |
| 1209 | // Send two packets before packet size change. |
| 1210 | CreateData(data_len); |
| 1211 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1212 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1213 | iov_.iov_len, |
| 1214 | /*offset=*/0, NO_FIN); |
| 1215 | generator_.Flush(); |
| 1216 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1217 | EXPECT_FALSE(consumed.fin_consumed); |
| 1218 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1219 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1220 | |
| 1221 | // Make sure we already have two packets. |
| 1222 | ASSERT_EQ(2u, packets_.size()); |
| 1223 | |
| 1224 | // Increase packet size. |
| 1225 | generator_.SetMaxPacketLength(packet_len); |
| 1226 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1227 | |
| 1228 | // Send a packet after packet size change. |
| 1229 | CreateData(data_len); |
| 1230 | generator_.AttachPacketFlusher(); |
| 1231 | consumed = generator_.ConsumeData( |
| 1232 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1233 | iov_.iov_len, data_len, FIN); |
| 1234 | generator_.Flush(); |
| 1235 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1236 | EXPECT_TRUE(consumed.fin_consumed); |
| 1237 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1238 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1239 | |
| 1240 | // We expect first data chunk to get fragmented, but the second one to fit |
| 1241 | // into a single packet. |
| 1242 | ASSERT_EQ(3u, packets_.size()); |
| 1243 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1244 | EXPECT_LE(kDefaultMaxPacketSize, packets_[2].encrypted_length); |
| 1245 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1246 | } |
| 1247 | |
| 1248 | // Test whether SetMaxPacketLength() works correctly when we force the change of |
| 1249 | // the packet size in the middle of the batched packet. |
| 1250 | TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) { |
| 1251 | delegate_.SetCanWriteAnything(); |
| 1252 | |
| 1253 | size_t first_write_len = kDefaultMaxPacketSize / 2; |
| 1254 | size_t packet_len = kDefaultMaxPacketSize + 100; |
| 1255 | size_t second_write_len = packet_len + 1; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1256 | ASSERT_LE(packet_len, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1257 | |
| 1258 | // First send half of the packet worth of data. We are in the batch mode, so |
| 1259 | // should not cause packet serialization. |
| 1260 | CreateData(first_write_len); |
| 1261 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1262 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1263 | iov_.iov_len, |
| 1264 | /*offset=*/0, NO_FIN); |
| 1265 | EXPECT_EQ(first_write_len, consumed.bytes_consumed); |
| 1266 | EXPECT_FALSE(consumed.fin_consumed); |
| 1267 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1268 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1269 | |
| 1270 | // Make sure we have no packets so far. |
| 1271 | ASSERT_EQ(0u, packets_.size()); |
| 1272 | |
| 1273 | // Expect a packet to be flushed. |
| 1274 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1275 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1276 | |
| 1277 | // Increase packet size after flushing all frames. |
| 1278 | // Ensure it's immediately enacted. |
| 1279 | generator_.FlushAllQueuedFrames(); |
| 1280 | generator_.SetMaxPacketLength(packet_len); |
| 1281 | EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); |
| 1282 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1283 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1284 | |
| 1285 | // We expect to see exactly one packet serialized after that, because we send |
| 1286 | // a value somewhat exceeding new max packet size, and the tail data does not |
| 1287 | // get serialized because we are still in the batch mode. |
| 1288 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1289 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1290 | |
| 1291 | // Send a more than a packet worth of data to the same stream. This should |
| 1292 | // trigger serialization of one packet, and queue another one. |
| 1293 | CreateData(second_write_len); |
| 1294 | consumed = generator_.ConsumeData( |
| 1295 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1296 | iov_.iov_len, |
| 1297 | /*offset=*/first_write_len, FIN); |
| 1298 | EXPECT_EQ(second_write_len, consumed.bytes_consumed); |
| 1299 | EXPECT_TRUE(consumed.fin_consumed); |
| 1300 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1301 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1302 | |
| 1303 | // We expect the first packet to be underfilled, and the second packet be up |
| 1304 | // to the new max packet size. |
| 1305 | ASSERT_EQ(2u, packets_.size()); |
| 1306 | EXPECT_GT(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1307 | EXPECT_EQ(packet_len, packets_[1].encrypted_length); |
| 1308 | |
| 1309 | CheckAllPacketsHaveSingleStreamFrame(); |
| 1310 | } |
| 1311 | |
| 1312 | // Test sending a connectivity probing packet. |
| 1313 | TEST_F(QuicPacketGeneratorTest, GenerateConnectivityProbingPacket) { |
| 1314 | delegate_.SetCanWriteAnything(); |
| 1315 | |
| 1316 | OwningSerializedPacketPointer probing_packet; |
| 1317 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1318 | QuicPathFrameBuffer payload = { |
| 1319 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 1320 | probing_packet = |
| 1321 | generator_.SerializePathChallengeConnectivityProbingPacket(&payload); |
| 1322 | } else { |
| 1323 | probing_packet = generator_.SerializeConnectivityProbingPacket(); |
| 1324 | } |
| 1325 | |
| 1326 | ASSERT_TRUE(simple_framer_.ProcessPacket(QuicEncryptedPacket( |
| 1327 | probing_packet->encrypted_buffer, probing_packet->encrypted_length))); |
| 1328 | |
| 1329 | EXPECT_EQ(2u, simple_framer_.num_frames()); |
| 1330 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1331 | EXPECT_EQ(1u, simple_framer_.path_challenge_frames().size()); |
| 1332 | } else { |
| 1333 | EXPECT_EQ(1u, simple_framer_.ping_frames().size()); |
| 1334 | } |
| 1335 | EXPECT_EQ(1u, simple_framer_.padding_frames().size()); |
| 1336 | } |
| 1337 | |
| 1338 | // Test sending an MTU probe, without any surrounding data. |
| 1339 | TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_Simple) { |
| 1340 | delegate_.SetCanWriteAnything(); |
| 1341 | |
| 1342 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1343 | static_assert(target_mtu < kMaxOutgoingPacketSize, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1344 | "The MTU probe used by the test exceeds maximum packet size"); |
| 1345 | |
| 1346 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1347 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1348 | |
| 1349 | generator_.GenerateMtuDiscoveryPacket(target_mtu); |
| 1350 | |
| 1351 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1352 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1353 | ASSERT_EQ(1u, packets_.size()); |
| 1354 | EXPECT_EQ(target_mtu, packets_[0].encrypted_length); |
| 1355 | |
| 1356 | PacketContents contents; |
| 1357 | contents.num_mtu_discovery_frames = 1; |
| 1358 | contents.num_padding_frames = 1; |
| 1359 | CheckPacketContains(contents, 0); |
| 1360 | } |
| 1361 | |
| 1362 | // Test sending an MTU probe. Surround it with data, to ensure that it resets |
| 1363 | // the MTU to the value before the probe was sent. |
| 1364 | TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_SurroundedByData) { |
| 1365 | delegate_.SetCanWriteAnything(); |
| 1366 | |
| 1367 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1368 | static_assert(target_mtu < kMaxOutgoingPacketSize, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1369 | "The MTU probe used by the test exceeds maximum packet size"); |
| 1370 | |
| 1371 | // Send enough data so it would always cause two packets to be sent. |
| 1372 | const size_t data_len = target_mtu + 1; |
| 1373 | |
| 1374 | // Send a total of five packets: two packets before the probe, the probe |
| 1375 | // itself, and two packets after the probe. |
| 1376 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1377 | .Times(5) |
| 1378 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1379 | |
| 1380 | // Send data before the MTU probe. |
| 1381 | CreateData(data_len); |
| 1382 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1383 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1384 | iov_.iov_len, |
| 1385 | /*offset=*/0, NO_FIN); |
| 1386 | generator_.Flush(); |
| 1387 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1388 | EXPECT_FALSE(consumed.fin_consumed); |
| 1389 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1390 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1391 | |
| 1392 | // Send the MTU probe. |
| 1393 | generator_.GenerateMtuDiscoveryPacket(target_mtu); |
| 1394 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1395 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1396 | |
| 1397 | // Send data after the MTU probe. |
| 1398 | CreateData(data_len); |
| 1399 | generator_.AttachPacketFlusher(); |
| 1400 | consumed = generator_.ConsumeData( |
| 1401 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1402 | iov_.iov_len, |
| 1403 | /*offset=*/data_len, FIN); |
| 1404 | generator_.Flush(); |
| 1405 | EXPECT_EQ(data_len, consumed.bytes_consumed); |
| 1406 | EXPECT_TRUE(consumed.fin_consumed); |
| 1407 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1408 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1409 | |
| 1410 | ASSERT_EQ(5u, packets_.size()); |
| 1411 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); |
| 1412 | EXPECT_EQ(target_mtu, packets_[2].encrypted_length); |
| 1413 | EXPECT_EQ(kDefaultMaxPacketSize, packets_[3].encrypted_length); |
| 1414 | |
| 1415 | PacketContents probe_contents; |
| 1416 | probe_contents.num_mtu_discovery_frames = 1; |
| 1417 | probe_contents.num_padding_frames = 1; |
| 1418 | |
| 1419 | CheckPacketHasSingleStreamFrame(0); |
| 1420 | CheckPacketHasSingleStreamFrame(1); |
| 1421 | CheckPacketContains(probe_contents, 2); |
| 1422 | CheckPacketHasSingleStreamFrame(3); |
| 1423 | CheckPacketHasSingleStreamFrame(4); |
| 1424 | } |
| 1425 | |
| 1426 | TEST_F(QuicPacketGeneratorTest, DontCrashOnInvalidStopWaiting) { |
| 1427 | if (framer_.transport_version() > QUIC_VERSION_43) { |
| 1428 | return; |
| 1429 | } |
| 1430 | // Test added to ensure the generator does not crash when an invalid frame is |
| 1431 | // added. Because this is an indication of internal programming errors, |
| 1432 | // DFATALs are expected. |
| 1433 | // A 1 byte packet number length can't encode a gap of 1000. |
| 1434 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1000); |
| 1435 | |
| 1436 | delegate_.SetCanNotWrite(); |
| 1437 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1438 | generator_.SetShouldSendAck(true); |
| 1439 | } |
| 1440 | delegate_.SetCanWriteAnything(); |
| 1441 | |
| 1442 | if (!GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode)) { |
| 1443 | // Set up frames to write into the creator when control frames are written. |
| 1444 | EXPECT_CALL(delegate_, GetUpdatedAckFrame()) |
| 1445 | .WillOnce(Return(QuicFrame(&ack_frame_))); |
| 1446 | EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_)); |
| 1447 | // Generator should have queued control frames, and creator should be empty. |
| 1448 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1449 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1450 | EXPECT_FALSE(creator_->HasPendingFrames()); |
| 1451 | } |
| 1452 | |
| 1453 | // This will not serialize any packets, because of the invalid frame. |
| 1454 | EXPECT_CALL(delegate_, |
| 1455 | OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, _, |
| 1456 | ConnectionCloseSource::FROM_SELF)); |
| 1457 | EXPECT_QUIC_BUG(generator_.Flush(), |
| 1458 | "packet_number_length 1 is too small " |
| 1459 | "for least_unacked_delta: 1001"); |
| 1460 | } |
| 1461 | |
| 1462 | // Regression test for b/31486443. |
| 1463 | TEST_F(QuicPacketGeneratorTest, ConnectionCloseFrameLargerThanPacketSize) { |
| 1464 | delegate_.SetCanWriteAnything(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1465 | char buf[2000] = {}; |
| 1466 | QuicStringPiece error_details(buf, 2000); |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 1467 | QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame( |
| 1468 | QUIC_PACKET_WRITE_ERROR, std::string(error_details)); |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 1469 | if (framer_.transport_version() == QUIC_VERSION_99) { |
| 1470 | frame->close_type = IETF_QUIC_TRANSPORT_CONNECTION_CLOSE; |
| 1471 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1472 | generator_.ConsumeRetransmittableControlFrame(QuicFrame(frame), |
| 1473 | /*bundle_ack=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1474 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1475 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1476 | } |
| 1477 | |
| 1478 | TEST_F(QuicPacketGeneratorTest, RandomPaddingAfterFinSingleStreamSinglePacket) { |
| 1479 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1480 | char buf[kStreamFramePayloadSize] = {}; |
| 1481 | const QuicStreamId kDataStreamId = 5; |
| 1482 | // Set the packet size be enough for one stream frame with 0 stream offset and |
| 1483 | // max size of random padding. |
| 1484 | size_t length = |
| 1485 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1486 | GetPacketHeaderSize( |
| 1487 | framer_.transport_version(), |
| 1488 | creator_->GetDestinationConnectionIdLength(), |
| 1489 | creator_->GetSourceConnectionIdLength(), |
| 1490 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1491 | !kIncludeDiversificationNonce, |
| 1492 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1493 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1494 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1495 | QuicFramer::GetMinStreamFrameSize( |
| 1496 | framer_.transport_version(), kDataStreamId, 0, |
| 1497 | /*last_frame_in_packet=*/false, |
| 1498 | kStreamFramePayloadSize + kMaxNumRandomPaddingBytes) + |
| 1499 | kStreamFramePayloadSize + kMaxNumRandomPaddingBytes; |
| 1500 | generator_.SetMaxPacketLength(length); |
| 1501 | delegate_.SetCanWriteAnything(); |
| 1502 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1503 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1504 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1505 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1506 | kDataStreamId, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1507 | generator_.Flush(); |
| 1508 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1509 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1510 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1511 | |
| 1512 | EXPECT_EQ(1u, packets_.size()); |
| 1513 | PacketContents contents; |
| 1514 | // The packet has both stream and padding frames. |
| 1515 | contents.num_padding_frames = 1; |
| 1516 | contents.num_stream_frames = 1; |
| 1517 | CheckPacketContains(contents, 0); |
| 1518 | } |
| 1519 | |
| 1520 | TEST_F(QuicPacketGeneratorTest, |
| 1521 | RandomPaddingAfterFinSingleStreamMultiplePackets) { |
| 1522 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1523 | char buf[kStreamFramePayloadSize] = {}; |
| 1524 | const QuicStreamId kDataStreamId = 5; |
| 1525 | // Set the packet size be enough for one stream frame with 0 stream offset + |
| 1526 | // 1. One or more packets will accommodate. |
| 1527 | size_t length = |
| 1528 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1529 | GetPacketHeaderSize( |
| 1530 | framer_.transport_version(), |
| 1531 | creator_->GetDestinationConnectionIdLength(), |
| 1532 | creator_->GetSourceConnectionIdLength(), |
| 1533 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1534 | !kIncludeDiversificationNonce, |
| 1535 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1536 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1537 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1538 | QuicFramer::GetMinStreamFrameSize( |
| 1539 | framer_.transport_version(), kDataStreamId, 0, |
| 1540 | /*last_frame_in_packet=*/false, kStreamFramePayloadSize + 1) + |
| 1541 | kStreamFramePayloadSize + 1; |
| 1542 | generator_.SetMaxPacketLength(length); |
| 1543 | delegate_.SetCanWriteAnything(); |
| 1544 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1545 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1546 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1547 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1548 | kDataStreamId, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1549 | generator_.Flush(); |
| 1550 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1551 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1552 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1553 | |
| 1554 | EXPECT_LE(1u, packets_.size()); |
| 1555 | PacketContents contents; |
| 1556 | // The first packet has both stream and padding frames. |
| 1557 | contents.num_stream_frames = 1; |
| 1558 | contents.num_padding_frames = 1; |
| 1559 | CheckPacketContains(contents, 0); |
| 1560 | |
| 1561 | for (size_t i = 1; i < packets_.size(); ++i) { |
| 1562 | // Following packets only have paddings. |
| 1563 | contents.num_stream_frames = 0; |
| 1564 | contents.num_padding_frames = 1; |
| 1565 | CheckPacketContains(contents, i); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | TEST_F(QuicPacketGeneratorTest, |
| 1570 | RandomPaddingAfterFinMultipleStreamsMultiplePackets) { |
| 1571 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1572 | char buf[kStreamFramePayloadSize] = {}; |
| 1573 | const QuicStreamId kDataStreamId1 = 5; |
| 1574 | const QuicStreamId kDataStreamId2 = 6; |
| 1575 | // Set the packet size be enough for first frame with 0 stream offset + second |
| 1576 | // frame + 1 byte payload. two or more packets will accommodate. |
| 1577 | size_t length = |
| 1578 | NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) + |
| 1579 | GetPacketHeaderSize( |
| 1580 | framer_.transport_version(), |
| 1581 | creator_->GetDestinationConnectionIdLength(), |
| 1582 | creator_->GetSourceConnectionIdLength(), |
| 1583 | QuicPacketCreatorPeer::SendVersionInPacket(creator_), |
| 1584 | !kIncludeDiversificationNonce, |
| 1585 | QuicPacketCreatorPeer::GetPacketNumberLength(creator_), |
| 1586 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(creator_), 0, |
| 1587 | QuicPacketCreatorPeer::GetLengthLength(creator_)) + |
| 1588 | QuicFramer::GetMinStreamFrameSize( |
| 1589 | framer_.transport_version(), kDataStreamId1, 0, |
| 1590 | /*last_frame_in_packet=*/false, kStreamFramePayloadSize) + |
| 1591 | kStreamFramePayloadSize + |
| 1592 | QuicFramer::GetMinStreamFrameSize(framer_.transport_version(), |
| 1593 | kDataStreamId1, 0, |
| 1594 | /*last_frame_in_packet=*/false, 1) + |
| 1595 | 1; |
| 1596 | generator_.SetMaxPacketLength(length); |
| 1597 | delegate_.SetCanWriteAnything(); |
| 1598 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1599 | .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1600 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1601 | QuicConsumedData consumed = generator_.ConsumeData( |
| 1602 | kDataStreamId1, &iov_, 1u, iov_.iov_len, 0, FIN_AND_PADDING); |
| 1603 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1604 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
| 1605 | consumed = generator_.ConsumeData(kDataStreamId2, &iov_, 1u, iov_.iov_len, 0, |
| 1606 | FIN_AND_PADDING); |
| 1607 | EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed); |
| 1608 | generator_.Flush(); |
| 1609 | EXPECT_FALSE(generator_.HasQueuedFrames()); |
| 1610 | EXPECT_FALSE(generator_.HasRetransmittableFrames()); |
| 1611 | |
| 1612 | EXPECT_LE(2u, packets_.size()); |
| 1613 | PacketContents contents; |
| 1614 | // The first packet has two stream frames. |
| 1615 | contents.num_stream_frames = 2; |
| 1616 | CheckPacketContains(contents, 0); |
| 1617 | |
| 1618 | // The second packet has one stream frame and padding frames. |
| 1619 | contents.num_stream_frames = 1; |
| 1620 | contents.num_padding_frames = 1; |
| 1621 | CheckPacketContains(contents, 1); |
| 1622 | |
| 1623 | for (size_t i = 2; i < packets_.size(); ++i) { |
| 1624 | // Following packets only have paddings. |
| 1625 | contents.num_stream_frames = 0; |
| 1626 | contents.num_padding_frames = 1; |
| 1627 | CheckPacketContains(contents, i); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | TEST_F(QuicPacketGeneratorTest, AddMessageFrame) { |
| 1632 | if (framer_.transport_version() <= QUIC_VERSION_44) { |
| 1633 | return; |
| 1634 | } |
| 1635 | quic::QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 1636 | delegate_.SetCanWriteAnything(); |
| 1637 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1638 | .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); |
| 1639 | |
| 1640 | MakeIOVector("foo", &iov_); |
| 1641 | generator_.ConsumeData( |
| 1642 | QuicUtils::GetHeadersStreamId(framer_.transport_version()), &iov_, 1u, |
| 1643 | iov_.iov_len, 0, FIN); |
| 1644 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, |
| 1645 | generator_.AddMessageFrame( |
| 1646 | 1, MakeSpan(&allocator_, "message", &storage))); |
| 1647 | EXPECT_TRUE(generator_.HasQueuedFrames()); |
| 1648 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1649 | |
| 1650 | // Add a message which causes the flush of current packet. |
| 1651 | EXPECT_EQ( |
| 1652 | MESSAGE_STATUS_SUCCESS, |
| 1653 | generator_.AddMessageFrame( |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1654 | 2, MakeSpan( |
| 1655 | &allocator_, |
| 1656 | std::string(generator_.GetCurrentLargestMessagePayload(), 'a'), |
| 1657 | &storage))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1658 | EXPECT_TRUE(generator_.HasRetransmittableFrames()); |
| 1659 | |
| 1660 | // Failed to send messages which cannot fit into one packet. |
| 1661 | EXPECT_EQ( |
| 1662 | MESSAGE_STATUS_TOO_LARGE, |
| 1663 | generator_.AddMessageFrame( |
| 1664 | 3, |
| 1665 | MakeSpan(&allocator_, |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1666 | std::string( |
| 1667 | generator_.GetCurrentLargestMessagePayload() + 10, 'a'), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1668 | &storage))); |
| 1669 | } |
| 1670 | |
| 1671 | } // namespace test |
| 1672 | } // namespace quic |