Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: net/quic/core/quic_packet_generator_test.cc

Issue 2828543002: Let QUIC be able to add random padding of [1,256] bytes after fin is consumed for a stream. Random … (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/core/quic_packet_generator.cc ('k') | net/quic/core/quic_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/core/quic_packet_generator.h" 5 #include "net/quic/core/quic_packet_generator.h"
6 6
7 #include <cstdint> 7 #include <cstdint>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "net/quic/core/crypto/crypto_protocol.h" 12 #include "net/quic/core/crypto/crypto_protocol.h"
13 #include "net/quic/core/crypto/null_encrypter.h" 13 #include "net/quic/core/crypto/null_encrypter.h"
14 #include "net/quic/core/crypto/quic_decrypter.h" 14 #include "net/quic/core/crypto/quic_decrypter.h"
15 #include "net/quic/core/crypto/quic_encrypter.h" 15 #include "net/quic/core/crypto/quic_encrypter.h"
16 #include "net/quic/core/quic_simple_buffer_allocator.h" 16 #include "net/quic/core/quic_simple_buffer_allocator.h"
17 #include "net/quic/core/quic_utils.h" 17 #include "net/quic/core/quic_utils.h"
18 #include "net/quic/platform/api/quic_socket_address.h" 18 #include "net/quic/platform/api/quic_socket_address.h"
19 #include "net/quic/platform/api/quic_string_piece.h" 19 #include "net/quic/platform/api/quic_string_piece.h"
20 #include "net/quic/test_tools/mock_random.h"
20 #include "net/quic/test_tools/quic_packet_creator_peer.h" 21 #include "net/quic/test_tools/quic_packet_creator_peer.h"
21 #include "net/quic/test_tools/quic_packet_generator_peer.h" 22 #include "net/quic/test_tools/quic_packet_generator_peer.h"
22 #include "net/quic/test_tools/quic_test_utils.h" 23 #include "net/quic/test_tools/quic_test_utils.h"
23 #include "net/quic/test_tools/simple_quic_framer.h" 24 #include "net/quic/test_tools/simple_quic_framer.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 using std::string; 28 using std::string;
28 using testing::InSequence; 29 using testing::InSequence;
29 using testing::Return; 30 using testing::Return;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // contains the expected frames. 79 // contains the expected frames.
79 struct PacketContents { 80 struct PacketContents {
80 PacketContents() 81 PacketContents()
81 : num_ack_frames(0), 82 : num_ack_frames(0),
82 num_connection_close_frames(0), 83 num_connection_close_frames(0),
83 num_goaway_frames(0), 84 num_goaway_frames(0),
84 num_rst_stream_frames(0), 85 num_rst_stream_frames(0),
85 num_stop_waiting_frames(0), 86 num_stop_waiting_frames(0),
86 num_stream_frames(0), 87 num_stream_frames(0),
87 num_ping_frames(0), 88 num_ping_frames(0),
88 num_mtu_discovery_frames(0) {} 89 num_mtu_discovery_frames(0),
90 num_padding_frames(0) {}
89 91
90 size_t num_ack_frames; 92 size_t num_ack_frames;
91 size_t num_connection_close_frames; 93 size_t num_connection_close_frames;
92 size_t num_goaway_frames; 94 size_t num_goaway_frames;
93 size_t num_rst_stream_frames; 95 size_t num_rst_stream_frames;
94 size_t num_stop_waiting_frames; 96 size_t num_stop_waiting_frames;
95 size_t num_stream_frames; 97 size_t num_stream_frames;
96 size_t num_ping_frames; 98 size_t num_ping_frames;
97 size_t num_mtu_discovery_frames; 99 size_t num_mtu_discovery_frames;
100 size_t num_padding_frames;
98 }; 101 };
99 102
100 } // namespace 103 } // namespace
101 104
102 class QuicPacketGeneratorTest : public ::testing::Test { 105 class QuicPacketGeneratorTest : public ::testing::Test {
103 public: 106 public:
104 QuicPacketGeneratorTest() 107 QuicPacketGeneratorTest()
105 : framer_(AllSupportedVersions(), 108 : framer_(AllSupportedVersions(),
106 QuicTime::Zero(), 109 QuicTime::Zero(),
107 Perspective::IS_CLIENT), 110 Perspective::IS_CLIENT),
108 generator_(42, &framer_, &buffer_allocator_, &delegate_), 111 generator_(42,
112 &framer_,
113 &random_generator_,
114 &buffer_allocator_,
115 &delegate_),
109 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)) { 116 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)) {
110 creator_->SetEncrypter(ENCRYPTION_FORWARD_SECURE, 117 creator_->SetEncrypter(ENCRYPTION_FORWARD_SECURE,
111 new NullEncrypter(Perspective::IS_CLIENT)); 118 new NullEncrypter(Perspective::IS_CLIENT));
112 creator_->set_encryption_level(ENCRYPTION_FORWARD_SECURE); 119 creator_->set_encryption_level(ENCRYPTION_FORWARD_SECURE);
113 } 120 }
114 121
115 ~QuicPacketGeneratorTest() override { 122 ~QuicPacketGeneratorTest() override {
116 for (SerializedPacket& packet : packets_) { 123 for (SerializedPacket& packet : packets_) {
117 delete[] packet.encrypted_buffer; 124 delete[] packet.encrypted_buffer;
118 ClearSerializedPacket(&packet); 125 ClearSerializedPacket(&packet);
(...skipping 19 matching lines...) Expand all
138 void CheckPacketContains(const PacketContents& contents, 145 void CheckPacketContains(const PacketContents& contents,
139 size_t packet_index) { 146 size_t packet_index) {
140 ASSERT_GT(packets_.size(), packet_index); 147 ASSERT_GT(packets_.size(), packet_index);
141 const SerializedPacket& packet = packets_[packet_index]; 148 const SerializedPacket& packet = packets_[packet_index];
142 size_t num_retransmittable_frames = 149 size_t num_retransmittable_frames =
143 contents.num_connection_close_frames + contents.num_goaway_frames + 150 contents.num_connection_close_frames + contents.num_goaway_frames +
144 contents.num_rst_stream_frames + contents.num_stream_frames + 151 contents.num_rst_stream_frames + contents.num_stream_frames +
145 contents.num_ping_frames; 152 contents.num_ping_frames;
146 size_t num_frames = 153 size_t num_frames =
147 contents.num_ack_frames + contents.num_stop_waiting_frames + 154 contents.num_ack_frames + contents.num_stop_waiting_frames +
148 contents.num_mtu_discovery_frames + num_retransmittable_frames; 155 contents.num_mtu_discovery_frames + contents.num_padding_frames +
156 num_retransmittable_frames;
149 157
150 if (num_retransmittable_frames == 0) { 158 if (num_retransmittable_frames == 0) {
151 ASSERT_TRUE(packet.retransmittable_frames.empty()); 159 ASSERT_TRUE(packet.retransmittable_frames.empty());
152 } else { 160 } else {
153 ASSERT_FALSE(packet.retransmittable_frames.empty()); 161 ASSERT_FALSE(packet.retransmittable_frames.empty());
154 EXPECT_EQ(num_retransmittable_frames, 162 EXPECT_EQ(num_retransmittable_frames,
155 packet.retransmittable_frames.size()); 163 packet.retransmittable_frames.size());
156 } 164 }
157 165
158 ASSERT_TRUE(packet.encrypted_buffer != nullptr); 166 ASSERT_TRUE(packet.encrypted_buffer != nullptr);
159 ASSERT_TRUE(simple_framer_.ProcessPacket( 167 ASSERT_TRUE(simple_framer_.ProcessPacket(
160 QuicEncryptedPacket(packet.encrypted_buffer, packet.encrypted_length))); 168 QuicEncryptedPacket(packet.encrypted_buffer, packet.encrypted_length)));
161 EXPECT_EQ(num_frames, simple_framer_.num_frames()); 169 EXPECT_EQ(num_frames, simple_framer_.num_frames());
162 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size()); 170 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
163 EXPECT_EQ(contents.num_connection_close_frames, 171 EXPECT_EQ(contents.num_connection_close_frames,
164 simple_framer_.connection_close_frames().size()); 172 simple_framer_.connection_close_frames().size());
165 EXPECT_EQ(contents.num_goaway_frames, 173 EXPECT_EQ(contents.num_goaway_frames,
166 simple_framer_.goaway_frames().size()); 174 simple_framer_.goaway_frames().size());
167 EXPECT_EQ(contents.num_rst_stream_frames, 175 EXPECT_EQ(contents.num_rst_stream_frames,
168 simple_framer_.rst_stream_frames().size()); 176 simple_framer_.rst_stream_frames().size());
169 EXPECT_EQ(contents.num_stream_frames, 177 EXPECT_EQ(contents.num_stream_frames,
170 simple_framer_.stream_frames().size()); 178 simple_framer_.stream_frames().size());
171 EXPECT_EQ(contents.num_stop_waiting_frames, 179 EXPECT_EQ(contents.num_stop_waiting_frames,
172 simple_framer_.stop_waiting_frames().size()); 180 simple_framer_.stop_waiting_frames().size());
181 EXPECT_EQ(contents.num_padding_frames,
182 simple_framer_.padding_frames().size());
173 183
174 // From the receiver's perspective, MTU discovery frames are ping frames. 184 // From the receiver's perspective, MTU discovery frames are ping frames.
175 EXPECT_EQ(contents.num_ping_frames + contents.num_mtu_discovery_frames, 185 EXPECT_EQ(contents.num_ping_frames + contents.num_mtu_discovery_frames,
176 simple_framer_.ping_frames().size()); 186 simple_framer_.ping_frames().size());
177 } 187 }
178 188
179 void CheckPacketHasSingleStreamFrame(size_t packet_index) { 189 void CheckPacketHasSingleStreamFrame(size_t packet_index) {
180 ASSERT_GT(packets_.size(), packet_index); 190 ASSERT_GT(packets_.size(), packet_index);
181 const SerializedPacket& packet = packets_[packet_index]; 191 const SerializedPacket& packet = packets_[packet_index];
182 ASSERT_FALSE(packet.retransmittable_frames.empty()); 192 ASSERT_FALSE(packet.retransmittable_frames.empty());
(...skipping 17 matching lines...) Expand all
200 iov_.iov_base = data_array_.get(); 210 iov_.iov_base = data_array_.get();
201 iov_.iov_len = len; 211 iov_.iov_len = len;
202 return QuicIOVector(&iov_, 1, len); 212 return QuicIOVector(&iov_, 1, len);
203 } 213 }
204 214
205 QuicIOVector MakeIOVectorFromStringPiece(QuicStringPiece s) { 215 QuicIOVector MakeIOVectorFromStringPiece(QuicStringPiece s) {
206 return MakeIOVector(s, &iov_); 216 return MakeIOVector(s, &iov_);
207 } 217 }
208 218
209 QuicFramer framer_; 219 QuicFramer framer_;
220 MockRandom random_generator_;
210 SimpleBufferAllocator buffer_allocator_; 221 SimpleBufferAllocator buffer_allocator_;
211 StrictMock<MockDelegate> delegate_; 222 StrictMock<MockDelegate> delegate_;
212 QuicPacketGenerator generator_; 223 QuicPacketGenerator generator_;
213 QuicPacketCreator* creator_; 224 QuicPacketCreator* creator_;
214 SimpleQuicFramer simple_framer_; 225 SimpleQuicFramer simple_framer_;
215 std::vector<SerializedPacket> packets_; 226 std::vector<SerializedPacket> packets_;
216 QuicAckFrame ack_frame_; 227 QuicAckFrame ack_frame_;
217 228
218 private: 229 private:
219 std::unique_ptr<char[]> data_array_; 230 std::unique_ptr<char[]> data_array_;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 346
336 PacketContents contents; 347 PacketContents contents;
337 contents.num_rst_stream_frames = 1; 348 contents.num_rst_stream_frames = 1;
338 CheckPacketContains(contents, 0); 349 CheckPacketContains(contents, 0);
339 } 350 }
340 351
341 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) { 352 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
342 delegate_.SetCanNotWrite(); 353 delegate_.SetCanNotWrite();
343 354
344 QuicConsumedData consumed = generator_.ConsumeData( 355 QuicConsumedData consumed = generator_.ConsumeData(
345 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, true, nullptr); 356 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, FIN, nullptr);
346 EXPECT_EQ(0u, consumed.bytes_consumed); 357 EXPECT_EQ(0u, consumed.bytes_consumed);
347 EXPECT_FALSE(consumed.fin_consumed); 358 EXPECT_FALSE(consumed.fin_consumed);
348 EXPECT_FALSE(generator_.HasQueuedFrames()); 359 EXPECT_FALSE(generator_.HasQueuedFrames());
349 } 360 }
350 361
351 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) { 362 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
352 delegate_.SetCanWriteAnything(); 363 delegate_.SetCanWriteAnything();
353 generator_.StartBatchOperations(); 364 generator_.StartBatchOperations();
354 365
355 QuicConsumedData consumed = generator_.ConsumeData( 366 QuicConsumedData consumed = generator_.ConsumeData(
356 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, true, nullptr); 367 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, FIN, nullptr);
357 EXPECT_EQ(3u, consumed.bytes_consumed); 368 EXPECT_EQ(3u, consumed.bytes_consumed);
358 EXPECT_TRUE(consumed.fin_consumed); 369 EXPECT_TRUE(consumed.fin_consumed);
359 EXPECT_TRUE(generator_.HasQueuedFrames()); 370 EXPECT_TRUE(generator_.HasQueuedFrames());
360 } 371 }
361 372
362 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) { 373 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
363 delegate_.SetCanWriteAnything(); 374 delegate_.SetCanWriteAnything();
364 375
365 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 376 EXPECT_CALL(delegate_, OnSerializedPacket(_))
366 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 377 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
367 QuicConsumedData consumed = generator_.ConsumeData( 378 QuicConsumedData consumed = generator_.ConsumeData(
368 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, true, nullptr); 379 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 2, FIN, nullptr);
369 EXPECT_EQ(3u, consumed.bytes_consumed); 380 EXPECT_EQ(3u, consumed.bytes_consumed);
370 EXPECT_TRUE(consumed.fin_consumed); 381 EXPECT_TRUE(consumed.fin_consumed);
371 EXPECT_FALSE(generator_.HasQueuedFrames()); 382 EXPECT_FALSE(generator_.HasQueuedFrames());
372 383
373 PacketContents contents; 384 PacketContents contents;
374 contents.num_stream_frames = 1; 385 contents.num_stream_frames = 1;
375 CheckPacketContains(contents, 0); 386 CheckPacketContains(contents, 0);
376 } 387 }
377 388
378 // Test the behavior of ConsumeData when the data consumed is for the crypto 389 // Test the behavior of ConsumeData when the data consumed is for the crypto
379 // handshake stream. Ensure that the packet is always sent and padded even if 390 // handshake stream. Ensure that the packet is always sent and padded even if
380 // the generator operates in batch mode. 391 // the generator operates in batch mode.
381 TEST_F(QuicPacketGeneratorTest, ConsumeData_Handshake) { 392 TEST_F(QuicPacketGeneratorTest, ConsumeData_Handshake) {
382 delegate_.SetCanWriteAnything(); 393 delegate_.SetCanWriteAnything();
383 generator_.StartBatchOperations(); 394 generator_.StartBatchOperations();
384 395
385 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 396 EXPECT_CALL(delegate_, OnSerializedPacket(_))
386 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 397 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
387 QuicConsumedData consumed = generator_.ConsumeData( 398 QuicConsumedData consumed = generator_.ConsumeData(
388 kCryptoStreamId, MakeIOVectorFromStringPiece("foo"), 0, false, nullptr); 399 kCryptoStreamId, MakeIOVectorFromStringPiece("foo"), 0, NO_FIN, nullptr);
389 EXPECT_EQ(3u, consumed.bytes_consumed); 400 EXPECT_EQ(3u, consumed.bytes_consumed);
390 EXPECT_FALSE(generator_.HasQueuedFrames()); 401 EXPECT_FALSE(generator_.HasQueuedFrames());
391 402
392 PacketContents contents; 403 PacketContents contents;
393 contents.num_stream_frames = 1; 404 contents.num_stream_frames = 1;
405 contents.num_padding_frames = 1;
394 CheckPacketContains(contents, 0); 406 CheckPacketContains(contents, 0);
395 407
396 ASSERT_EQ(1u, packets_.size()); 408 ASSERT_EQ(1u, packets_.size());
397 ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength()); 409 ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength());
398 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); 410 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length);
399 } 411 }
400 412
401 TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) { 413 TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) {
402 EXPECT_QUIC_BUG( 414 EXPECT_QUIC_BUG(
403 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece(""), 415 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece(""),
404 0, false, nullptr), 416 0, NO_FIN, nullptr),
405 "Attempt to consume empty data without FIN."); 417 "Attempt to consume empty data without FIN.");
406 } 418 }
407 419
408 TEST_F(QuicPacketGeneratorTest, 420 TEST_F(QuicPacketGeneratorTest,
409 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) { 421 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
410 delegate_.SetCanWriteAnything(); 422 delegate_.SetCanWriteAnything();
411 generator_.StartBatchOperations(); 423 generator_.StartBatchOperations();
412 424
413 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 425 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece("foo"),
414 2, true, nullptr); 426 2, FIN, nullptr);
415 QuicConsumedData consumed = generator_.ConsumeData( 427 QuicConsumedData consumed = generator_.ConsumeData(
416 3, MakeIOVectorFromStringPiece("quux"), 7, false, nullptr); 428 3, MakeIOVectorFromStringPiece("quux"), 7, NO_FIN, nullptr);
417 EXPECT_EQ(4u, consumed.bytes_consumed); 429 EXPECT_EQ(4u, consumed.bytes_consumed);
418 EXPECT_FALSE(consumed.fin_consumed); 430 EXPECT_FALSE(consumed.fin_consumed);
419 EXPECT_TRUE(generator_.HasQueuedFrames()); 431 EXPECT_TRUE(generator_.HasQueuedFrames());
420 } 432 }
421 433
422 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) { 434 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
423 delegate_.SetCanWriteAnything(); 435 delegate_.SetCanWriteAnything();
424 generator_.StartBatchOperations(); 436 generator_.StartBatchOperations();
425 437
426 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 438 generator_.ConsumeData(kHeadersStreamId, MakeIOVectorFromStringPiece("foo"),
427 2, true, nullptr); 439 2, FIN, nullptr);
428 QuicConsumedData consumed = generator_.ConsumeData( 440 QuicConsumedData consumed = generator_.ConsumeData(
429 3, MakeIOVectorFromStringPiece("quux"), 7, false, nullptr); 441 3, MakeIOVectorFromStringPiece("quux"), 7, NO_FIN, nullptr);
430 EXPECT_EQ(4u, consumed.bytes_consumed); 442 EXPECT_EQ(4u, consumed.bytes_consumed);
431 EXPECT_FALSE(consumed.fin_consumed); 443 EXPECT_FALSE(consumed.fin_consumed);
432 EXPECT_TRUE(generator_.HasQueuedFrames()); 444 EXPECT_TRUE(generator_.HasQueuedFrames());
433 445
434 // Now both frames will be flushed out. 446 // Now both frames will be flushed out.
435 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 447 EXPECT_CALL(delegate_, OnSerializedPacket(_))
436 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 448 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
437 generator_.FinishBatchOperations(); 449 generator_.FinishBatchOperations();
438 EXPECT_FALSE(generator_.HasQueuedFrames()); 450 EXPECT_FALSE(generator_.HasQueuedFrames());
439 451
(...skipping 21 matching lines...) Expand all
461 InSequence dummy; 473 InSequence dummy;
462 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 474 EXPECT_CALL(delegate_, OnSerializedPacket(_))
463 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 475 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
464 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 476 EXPECT_CALL(delegate_, OnSerializedPacket(_))
465 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 477 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
466 } 478 }
467 generator_.StartBatchOperations(); 479 generator_.StartBatchOperations();
468 // Queue enough data to prevent a stream frame with a non-zero offset from 480 // Queue enough data to prevent a stream frame with a non-zero offset from
469 // fitting. 481 // fitting.
470 QuicConsumedData consumed = generator_.ConsumeData( 482 QuicConsumedData consumed = generator_.ConsumeData(
471 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 0, false, nullptr); 483 kHeadersStreamId, MakeIOVectorFromStringPiece("foo"), 0, NO_FIN, nullptr);
472 EXPECT_EQ(3u, consumed.bytes_consumed); 484 EXPECT_EQ(3u, consumed.bytes_consumed);
473 EXPECT_FALSE(consumed.fin_consumed); 485 EXPECT_FALSE(consumed.fin_consumed);
474 EXPECT_TRUE(generator_.HasQueuedFrames()); 486 EXPECT_TRUE(generator_.HasQueuedFrames());
475 487
476 // This frame will not fit with the existing frame, causing the queued frame 488 // This frame will not fit with the existing frame, causing the queued frame
477 // to be serialized, and it will be added to a new open packet. 489 // to be serialized, and it will be added to a new open packet.
478 consumed = generator_.ConsumeData( 490 consumed = generator_.ConsumeData(
479 kHeadersStreamId, MakeIOVectorFromStringPiece("bar"), 3, true, nullptr); 491 kHeadersStreamId, MakeIOVectorFromStringPiece("bar"), 3, FIN, nullptr);
480 EXPECT_EQ(3u, consumed.bytes_consumed); 492 EXPECT_EQ(3u, consumed.bytes_consumed);
481 EXPECT_TRUE(consumed.fin_consumed); 493 EXPECT_TRUE(consumed.fin_consumed);
482 EXPECT_TRUE(generator_.HasQueuedFrames()); 494 EXPECT_TRUE(generator_.HasQueuedFrames());
483 495
484 creator_->Flush(); 496 creator_->Flush();
485 EXPECT_FALSE(generator_.HasQueuedFrames()); 497 EXPECT_FALSE(generator_.HasQueuedFrames());
486 498
487 PacketContents contents; 499 PacketContents contents;
488 contents.num_stream_frames = 1; 500 contents.num_stream_frames = 1;
489 CheckPacketContains(contents, 0); 501 CheckPacketContains(contents, 0);
(...skipping 27 matching lines...) Expand all
517 529
518 delegate_.SetCanWriteAnything(); 530 delegate_.SetCanWriteAnything();
519 531
520 generator_.StartBatchOperations(); 532 generator_.StartBatchOperations();
521 533
522 // When the first write operation is invoked, the ack frame will be returned. 534 // When the first write operation is invoked, the ack frame will be returned.
523 EXPECT_CALL(delegate_, GetUpdatedAckFrame()) 535 EXPECT_CALL(delegate_, GetUpdatedAckFrame())
524 .WillOnce(Return(QuicFrame(&ack_frame_))); 536 .WillOnce(Return(QuicFrame(&ack_frame_)));
525 537
526 // Send some data and a control frame 538 // Send some data and a control frame
527 generator_.ConsumeData(3, MakeIOVectorFromStringPiece("quux"), 7, false, 539 generator_.ConsumeData(3, MakeIOVectorFromStringPiece("quux"), 7, NO_FIN,
528 nullptr); 540 nullptr);
529 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame())); 541 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
530 542
531 // All five frames will be flushed out in a single packet. 543 // All five frames will be flushed out in a single packet.
532 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 544 EXPECT_CALL(delegate_, OnSerializedPacket(_))
533 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 545 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
534 generator_.FinishBatchOperations(); 546 generator_.FinishBatchOperations();
535 EXPECT_FALSE(generator_.HasQueuedFrames()); 547 EXPECT_FALSE(generator_.HasQueuedFrames());
536 548
537 PacketContents contents; 549 PacketContents contents;
(...skipping 24 matching lines...) Expand all
562 // All five frames will be flushed out in a single packet 574 // All five frames will be flushed out in a single packet
563 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 575 EXPECT_CALL(delegate_, OnSerializedPacket(_))
564 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 576 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
565 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 577 EXPECT_CALL(delegate_, OnSerializedPacket(_))
566 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 578 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
567 } 579 }
568 580
569 // Send enough data to exceed one packet 581 // Send enough data to exceed one packet
570 size_t data_len = kDefaultMaxPacketSize + 100; 582 size_t data_len = kDefaultMaxPacketSize + 100;
571 QuicConsumedData consumed = 583 QuicConsumedData consumed =
572 generator_.ConsumeData(3, CreateData(data_len), 0, true, nullptr); 584 generator_.ConsumeData(3, CreateData(data_len), 0, FIN, nullptr);
573 EXPECT_EQ(data_len, consumed.bytes_consumed); 585 EXPECT_EQ(data_len, consumed.bytes_consumed);
574 EXPECT_TRUE(consumed.fin_consumed); 586 EXPECT_TRUE(consumed.fin_consumed);
575 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame())); 587 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
576 588
577 generator_.FinishBatchOperations(); 589 generator_.FinishBatchOperations();
578 EXPECT_FALSE(generator_.HasQueuedFrames()); 590 EXPECT_FALSE(generator_.HasQueuedFrames());
579 591
580 // The first packet should have the queued data and part of the stream data. 592 // The first packet should have the queued data and part of the stream data.
581 PacketContents contents; 593 PacketContents contents;
582 contents.num_ack_frames = 1; 594 contents.num_ack_frames = 1;
(...skipping 28 matching lines...) Expand all
611 size_t packet_len = kDefaultMaxPacketSize + 100; 623 size_t packet_len = kDefaultMaxPacketSize + 100;
612 ASSERT_LE(packet_len, kMaxPacketSize); 624 ASSERT_LE(packet_len, kMaxPacketSize);
613 generator_.SetMaxPacketLength(packet_len); 625 generator_.SetMaxPacketLength(packet_len);
614 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); 626 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
615 627
616 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 628 EXPECT_CALL(delegate_, OnSerializedPacket(_))
617 .Times(3) 629 .Times(3)
618 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 630 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
619 QuicConsumedData consumed = 631 QuicConsumedData consumed =
620 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len), 632 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
621 /*offset=*/2, 633 /*offset=*/2, FIN, nullptr);
622 /*fin=*/true, nullptr);
623 EXPECT_EQ(data_len, consumed.bytes_consumed); 634 EXPECT_EQ(data_len, consumed.bytes_consumed);
624 EXPECT_TRUE(consumed.fin_consumed); 635 EXPECT_TRUE(consumed.fin_consumed);
625 EXPECT_FALSE(generator_.HasQueuedFrames()); 636 EXPECT_FALSE(generator_.HasQueuedFrames());
626 637
627 // We expect three packets, and first two of them have to be of packet_len 638 // We expect three packets, and first two of them have to be of packet_len
628 // size. We check multiple packets (instead of just one) because we want to 639 // size. We check multiple packets (instead of just one) because we want to
629 // ensure that |max_packet_length_| does not get changed incorrectly by the 640 // ensure that |max_packet_length_| does not get changed incorrectly by the
630 // generator after first packet is serialized. 641 // generator after first packet is serialized.
631 ASSERT_EQ(3u, packets_.size()); 642 ASSERT_EQ(3u, packets_.size());
632 EXPECT_EQ(packet_len, packets_[0].encrypted_length); 643 EXPECT_EQ(packet_len, packets_[0].encrypted_length);
(...skipping 13 matching lines...) Expand all
646 ASSERT_LE(packet_len, kMaxPacketSize); 657 ASSERT_LE(packet_len, kMaxPacketSize);
647 658
648 // We expect to see three packets in total. 659 // We expect to see three packets in total.
649 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 660 EXPECT_CALL(delegate_, OnSerializedPacket(_))
650 .Times(3) 661 .Times(3)
651 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 662 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
652 663
653 // Send two packets before packet size change. 664 // Send two packets before packet size change.
654 QuicConsumedData consumed = 665 QuicConsumedData consumed =
655 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len), 666 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
656 /*offset=*/2, 667 /*offset=*/2, NO_FIN, nullptr);
657 /*fin=*/false, nullptr);
658 EXPECT_EQ(data_len, consumed.bytes_consumed); 668 EXPECT_EQ(data_len, consumed.bytes_consumed);
659 EXPECT_FALSE(consumed.fin_consumed); 669 EXPECT_FALSE(consumed.fin_consumed);
660 EXPECT_FALSE(generator_.HasQueuedFrames()); 670 EXPECT_FALSE(generator_.HasQueuedFrames());
661 671
662 // Make sure we already have two packets. 672 // Make sure we already have two packets.
663 ASSERT_EQ(2u, packets_.size()); 673 ASSERT_EQ(2u, packets_.size());
664 674
665 // Increase packet size. 675 // Increase packet size.
666 generator_.SetMaxPacketLength(packet_len); 676 generator_.SetMaxPacketLength(packet_len);
667 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); 677 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
668 678
669 // Send a packet after packet size change. 679 // Send a packet after packet size change.
670 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len), 680 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
671 2 + data_len, 681 2 + data_len, FIN, nullptr);
672 /*fin=*/true, nullptr);
673 EXPECT_EQ(data_len, consumed.bytes_consumed); 682 EXPECT_EQ(data_len, consumed.bytes_consumed);
674 EXPECT_TRUE(consumed.fin_consumed); 683 EXPECT_TRUE(consumed.fin_consumed);
675 EXPECT_FALSE(generator_.HasQueuedFrames()); 684 EXPECT_FALSE(generator_.HasQueuedFrames());
676 685
677 // We expect first data chunk to get fragmented, but the second one to fit 686 // We expect first data chunk to get fragmented, but the second one to fit
678 // into a single packet. 687 // into a single packet.
679 ASSERT_EQ(3u, packets_.size()); 688 ASSERT_EQ(3u, packets_.size());
680 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); 689 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length);
681 EXPECT_LE(kDefaultMaxPacketSize, packets_[2].encrypted_length); 690 EXPECT_LE(kDefaultMaxPacketSize, packets_[2].encrypted_length);
682 CheckAllPacketsHaveSingleStreamFrame(); 691 CheckAllPacketsHaveSingleStreamFrame();
683 } 692 }
684 693
685 // Test whether SetMaxPacketLength() works correctly when we force the change of 694 // Test whether SetMaxPacketLength() works correctly when we force the change of
686 // the packet size in the middle of the batched packet. 695 // the packet size in the middle of the batched packet.
687 TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) { 696 TEST_F(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) {
688 delegate_.SetCanWriteAnything(); 697 delegate_.SetCanWriteAnything();
689 generator_.StartBatchOperations(); 698 generator_.StartBatchOperations();
690 699
691 size_t first_write_len = kDefaultMaxPacketSize / 2; 700 size_t first_write_len = kDefaultMaxPacketSize / 2;
692 size_t packet_len = kDefaultMaxPacketSize + 100; 701 size_t packet_len = kDefaultMaxPacketSize + 100;
693 size_t second_write_len = packet_len + 1; 702 size_t second_write_len = packet_len + 1;
694 ASSERT_LE(packet_len, kMaxPacketSize); 703 ASSERT_LE(packet_len, kMaxPacketSize);
695 704
696 // First send half of the packet worth of data. We are in the batch mode, so 705 // First send half of the packet worth of data. We are in the batch mode, so
697 // should not cause packet serialization. 706 // should not cause packet serialization.
698 QuicConsumedData consumed = 707 QuicConsumedData consumed =
699 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len), 708 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len),
700 /*offset=*/2, 709 /*offset=*/2, NO_FIN, nullptr);
701 /*fin=*/false, nullptr);
702 EXPECT_EQ(first_write_len, consumed.bytes_consumed); 710 EXPECT_EQ(first_write_len, consumed.bytes_consumed);
703 EXPECT_FALSE(consumed.fin_consumed); 711 EXPECT_FALSE(consumed.fin_consumed);
704 EXPECT_TRUE(generator_.HasQueuedFrames()); 712 EXPECT_TRUE(generator_.HasQueuedFrames());
705 713
706 // Make sure we have no packets so far. 714 // Make sure we have no packets so far.
707 ASSERT_EQ(0u, packets_.size()); 715 ASSERT_EQ(0u, packets_.size());
708 716
709 // Expect a packet to be flushed. 717 // Expect a packet to be flushed.
710 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 718 EXPECT_CALL(delegate_, OnSerializedPacket(_))
711 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 719 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
712 720
713 // Increase packet size after flushing all frames. 721 // Increase packet size after flushing all frames.
714 // Ensure it's immediately enacted. 722 // Ensure it's immediately enacted.
715 generator_.FlushAllQueuedFrames(); 723 generator_.FlushAllQueuedFrames();
716 generator_.SetMaxPacketLength(packet_len); 724 generator_.SetMaxPacketLength(packet_len);
717 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength()); 725 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
718 EXPECT_FALSE(generator_.HasQueuedFrames()); 726 EXPECT_FALSE(generator_.HasQueuedFrames());
719 727
720 // We expect to see exactly one packet serialized after that, because we send 728 // We expect to see exactly one packet serialized after that, because we send
721 // a value somewhat exceeding new max packet size, and the tail data does not 729 // a value somewhat exceeding new max packet size, and the tail data does not
722 // get serialized because we are still in the batch mode. 730 // get serialized because we are still in the batch mode.
723 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 731 EXPECT_CALL(delegate_, OnSerializedPacket(_))
724 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 732 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
725 733
726 // Send a more than a packet worth of data to the same stream. This should 734 // Send a more than a packet worth of data to the same stream. This should
727 // trigger serialization of one packet, and queue another one. 735 // trigger serialization of one packet, and queue another one.
728 consumed = 736 consumed =
729 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len), 737 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len),
730 /*offset=*/2 + first_write_len, 738 /*offset=*/2 + first_write_len, FIN, nullptr);
731 /*fin=*/true, nullptr);
732 EXPECT_EQ(second_write_len, consumed.bytes_consumed); 739 EXPECT_EQ(second_write_len, consumed.bytes_consumed);
733 EXPECT_TRUE(consumed.fin_consumed); 740 EXPECT_TRUE(consumed.fin_consumed);
734 EXPECT_TRUE(generator_.HasQueuedFrames()); 741 EXPECT_TRUE(generator_.HasQueuedFrames());
735 742
736 // We expect the first packet to be underfilled, and the second packet be up 743 // We expect the first packet to be underfilled, and the second packet be up
737 // to the new max packet size. 744 // to the new max packet size.
738 ASSERT_EQ(2u, packets_.size()); 745 ASSERT_EQ(2u, packets_.size());
739 EXPECT_GT(kDefaultMaxPacketSize, packets_[0].encrypted_length); 746 EXPECT_GT(kDefaultMaxPacketSize, packets_[0].encrypted_length);
740 EXPECT_EQ(packet_len, packets_[1].encrypted_length); 747 EXPECT_EQ(packet_len, packets_[1].encrypted_length);
741 748
(...skipping 12 matching lines...) Expand all
754 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 761 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
755 762
756 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr); 763 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr);
757 764
758 EXPECT_FALSE(generator_.HasQueuedFrames()); 765 EXPECT_FALSE(generator_.HasQueuedFrames());
759 ASSERT_EQ(1u, packets_.size()); 766 ASSERT_EQ(1u, packets_.size());
760 EXPECT_EQ(target_mtu, packets_[0].encrypted_length); 767 EXPECT_EQ(target_mtu, packets_[0].encrypted_length);
761 768
762 PacketContents contents; 769 PacketContents contents;
763 contents.num_mtu_discovery_frames = 1; 770 contents.num_mtu_discovery_frames = 1;
771 contents.num_padding_frames = 1;
764 CheckPacketContains(contents, 0); 772 CheckPacketContains(contents, 0);
765 } 773 }
766 774
767 // Test sending an MTU probe. Surround it with data, to ensure that it resets 775 // Test sending an MTU probe. Surround it with data, to ensure that it resets
768 // the MTU to the value before the probe was sent. 776 // the MTU to the value before the probe was sent.
769 TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_SurroundedByData) { 777 TEST_F(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_SurroundedByData) {
770 delegate_.SetCanWriteAnything(); 778 delegate_.SetCanWriteAnything();
771 779
772 const size_t target_mtu = kDefaultMaxPacketSize + 100; 780 const size_t target_mtu = kDefaultMaxPacketSize + 100;
773 static_assert(target_mtu < kMaxPacketSize, 781 static_assert(target_mtu < kMaxPacketSize,
774 "The MTU probe used by the test exceeds maximum packet size"); 782 "The MTU probe used by the test exceeds maximum packet size");
775 783
776 // Send enough data so it would always cause two packets to be sent. 784 // Send enough data so it would always cause two packets to be sent.
777 const size_t data_len = target_mtu + 1; 785 const size_t data_len = target_mtu + 1;
778 786
779 // Send a total of five packets: two packets before the probe, the probe 787 // Send a total of five packets: two packets before the probe, the probe
780 // itself, and two packets after the probe. 788 // itself, and two packets after the probe.
781 EXPECT_CALL(delegate_, OnSerializedPacket(_)) 789 EXPECT_CALL(delegate_, OnSerializedPacket(_))
782 .Times(5) 790 .Times(5)
783 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket)); 791 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
784 792
785 // Send data before the MTU probe. 793 // Send data before the MTU probe.
786 QuicConsumedData consumed = 794 QuicConsumedData consumed =
787 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len), 795 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
788 /*offset=*/2, 796 /*offset=*/2, NO_FIN, nullptr);
789 /*fin=*/false, nullptr);
790 EXPECT_EQ(data_len, consumed.bytes_consumed); 797 EXPECT_EQ(data_len, consumed.bytes_consumed);
791 EXPECT_FALSE(consumed.fin_consumed); 798 EXPECT_FALSE(consumed.fin_consumed);
792 EXPECT_FALSE(generator_.HasQueuedFrames()); 799 EXPECT_FALSE(generator_.HasQueuedFrames());
793 800
794 // Send the MTU probe. 801 // Send the MTU probe.
795 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr); 802 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr);
796 EXPECT_FALSE(generator_.HasQueuedFrames()); 803 EXPECT_FALSE(generator_.HasQueuedFrames());
797 804
798 // Send data after the MTU probe. 805 // Send data after the MTU probe.
799 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len), 806 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
800 /*offset=*/2 + data_len, 807 /*offset=*/2 + data_len, FIN, nullptr);
801 /*fin=*/true, nullptr);
802 EXPECT_EQ(data_len, consumed.bytes_consumed); 808 EXPECT_EQ(data_len, consumed.bytes_consumed);
803 EXPECT_TRUE(consumed.fin_consumed); 809 EXPECT_TRUE(consumed.fin_consumed);
804 EXPECT_FALSE(generator_.HasQueuedFrames()); 810 EXPECT_FALSE(generator_.HasQueuedFrames());
805 811
806 ASSERT_EQ(5u, packets_.size()); 812 ASSERT_EQ(5u, packets_.size());
807 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length); 813 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].encrypted_length);
808 EXPECT_EQ(target_mtu, packets_[2].encrypted_length); 814 EXPECT_EQ(target_mtu, packets_[2].encrypted_length);
809 EXPECT_EQ(kDefaultMaxPacketSize, packets_[3].encrypted_length); 815 EXPECT_EQ(kDefaultMaxPacketSize, packets_[3].encrypted_length);
810 816
811 PacketContents probe_contents; 817 PacketContents probe_contents;
812 probe_contents.num_mtu_discovery_frames = 1; 818 probe_contents.num_mtu_discovery_frames = 1;
819 probe_contents.num_padding_frames = 1;
813 820
814 CheckPacketHasSingleStreamFrame(0); 821 CheckPacketHasSingleStreamFrame(0);
815 CheckPacketHasSingleStreamFrame(1); 822 CheckPacketHasSingleStreamFrame(1);
816 CheckPacketContains(probe_contents, 2); 823 CheckPacketContains(probe_contents, 2);
817 CheckPacketHasSingleStreamFrame(3); 824 CheckPacketHasSingleStreamFrame(3);
818 CheckPacketHasSingleStreamFrame(4); 825 CheckPacketHasSingleStreamFrame(4);
819 } 826 }
820 827
821 TEST_F(QuicPacketGeneratorTest, DontCrashOnInvalidStopWaiting) { 828 TEST_F(QuicPacketGeneratorTest, DontCrashOnInvalidStopWaiting) {
822 // Test added to ensure the generator does not crash when an invalid frame is 829 // Test added to ensure the generator does not crash when an invalid frame is
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 char buf[2000] = {}; 862 char buf[2000] = {};
856 QuicStringPiece error_details(buf, 2000); 863 QuicStringPiece error_details(buf, 2000);
857 frame->error_details = error_details.as_string(); 864 frame->error_details = error_details.as_string();
858 EXPECT_CALL(delegate_, 865 EXPECT_CALL(delegate_,
859 OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, 866 OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET,
860 "Single frame cannot fit into a packet", _)); 867 "Single frame cannot fit into a packet", _));
861 EXPECT_QUIC_BUG(generator_.AddControlFrame(QuicFrame(frame)), ""); 868 EXPECT_QUIC_BUG(generator_.AddControlFrame(QuicFrame(frame)), "");
862 EXPECT_TRUE(generator_.HasQueuedFrames()); 869 EXPECT_TRUE(generator_.HasQueuedFrames());
863 } 870 }
864 871
872 TEST_F(QuicPacketGeneratorTest, RandomPaddingAfterFinSingleStreamSinglePacket) {
873 const QuicByteCount kStreamFramePayloadSize = 100u;
874 char buf[kStreamFramePayloadSize] = {};
875 const QuicStreamId kDataStreamId = 5;
876 // Set the packet size be enough for one stream frame with 0 stream offset and
877 // max size of random padding.
878 size_t length = NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) +
879 GetPacketHeaderSize(
880 framer_.version(), creator_->connection_id_length(),
881 kIncludeVersion, !kIncludeDiversificationNonce,
882 QuicPacketCreatorPeer::GetPacketNumberLength(creator_)) +
883 QuicFramer::GetMinStreamFrameSize(
884 kDataStreamId, 0, /*last_frame_in_packet=*/false) +
885 kStreamFramePayloadSize + kMaxNumRandomPaddingBytes;
886 generator_.SetMaxPacketLength(length);
887 delegate_.SetCanWriteAnything();
888 generator_.StartBatchOperations();
889 EXPECT_CALL(delegate_, OnSerializedPacket(_))
890 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
891 QuicConsumedData consumed =
892 generator_.ConsumeData(kDataStreamId,
893 MakeIOVectorFromStringPiece(
894 QuicStringPiece(buf, kStreamFramePayloadSize)),
895 0, FIN_AND_PADDING, nullptr);
896 generator_.FinishBatchOperations();
897 EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed);
898 EXPECT_FALSE(generator_.HasQueuedFrames());
899
900 EXPECT_EQ(1u, packets_.size());
901 PacketContents contents;
902 // The packet has both stream and padding frames.
903 contents.num_padding_frames = 1;
904 contents.num_stream_frames = 1;
905 CheckPacketContains(contents, 0);
906 }
907
908 TEST_F(QuicPacketGeneratorTest,
909 RandomPaddingAfterFinSingleStreamMultiplePackets) {
910 const QuicByteCount kStreamFramePayloadSize = 100u;
911 char buf[kStreamFramePayloadSize] = {};
912 const QuicStreamId kDataStreamId = 5;
913 // Set the packet size be enough for one stream frame with 0 stream offset +
914 // 1. One or more packets will accommodate.
915 size_t length = NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) +
916 GetPacketHeaderSize(
917 framer_.version(), creator_->connection_id_length(),
918 kIncludeVersion, !kIncludeDiversificationNonce,
919 QuicPacketCreatorPeer::GetPacketNumberLength(creator_)) +
920 QuicFramer::GetMinStreamFrameSize(
921 kDataStreamId, 0, /*last_frame_in_packet=*/false) +
922 kStreamFramePayloadSize + 1;
923 generator_.SetMaxPacketLength(length);
924 delegate_.SetCanWriteAnything();
925 generator_.StartBatchOperations();
926 EXPECT_CALL(delegate_, OnSerializedPacket(_))
927 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
928 QuicConsumedData consumed =
929 generator_.ConsumeData(kDataStreamId,
930 MakeIOVectorFromStringPiece(
931 QuicStringPiece(buf, kStreamFramePayloadSize)),
932 0, FIN_AND_PADDING, nullptr);
933 generator_.FinishBatchOperations();
934 EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed);
935 EXPECT_FALSE(generator_.HasQueuedFrames());
936
937 EXPECT_LE(1u, packets_.size());
938 PacketContents contents;
939 // The first packet has both stream and padding frames.
940 contents.num_stream_frames = 1;
941 contents.num_padding_frames = 1;
942 CheckPacketContains(contents, 0);
943
944 for (size_t i = 1; i < packets_.size(); ++i) {
945 // Following packets only have paddings.
946 contents.num_stream_frames = 0;
947 contents.num_padding_frames = 1;
948 CheckPacketContains(contents, i);
949 }
950 }
951
952 TEST_F(QuicPacketGeneratorTest,
953 RandomPaddingAfterFinMultipleStreamsMultiplePackets) {
954 const QuicByteCount kStreamFramePayloadSize = 100u;
955 char buf[kStreamFramePayloadSize] = {};
956 const QuicStreamId kDataStreamId1 = 5;
957 const QuicStreamId kDataStreamId2 = 6;
958 // Set the packet size be enough for first frame with 0 stream offset + second
959 // frame + 1 byte payload. two or more packets will accommodate.
960 size_t length = NullEncrypter(Perspective::IS_CLIENT).GetCiphertextSize(0) +
961 GetPacketHeaderSize(
962 framer_.version(), creator_->connection_id_length(),
963 kIncludeVersion, !kIncludeDiversificationNonce,
964 QuicPacketCreatorPeer::GetPacketNumberLength(creator_)) +
965 QuicFramer::GetMinStreamFrameSize(
966 kDataStreamId1, 0, /*last_frame_in_packet=*/false) +
967 kStreamFramePayloadSize +
968 QuicFramer::GetMinStreamFrameSize(
969 kDataStreamId1, 0, /*last_frame_in_packet=*/false) +
970 1;
971 generator_.SetMaxPacketLength(length);
972 delegate_.SetCanWriteAnything();
973 generator_.StartBatchOperations();
974 EXPECT_CALL(delegate_, OnSerializedPacket(_))
975 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
976 QuicConsumedData consumed =
977 generator_.ConsumeData(kDataStreamId1,
978 MakeIOVectorFromStringPiece(
979 QuicStringPiece(buf, kStreamFramePayloadSize)),
980 0, FIN_AND_PADDING, nullptr);
981 EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed);
982 consumed = generator_.ConsumeData(kDataStreamId2,
983 MakeIOVectorFromStringPiece(QuicStringPiece(
984 buf, kStreamFramePayloadSize)),
985 0, FIN_AND_PADDING, nullptr);
986 EXPECT_EQ(kStreamFramePayloadSize, consumed.bytes_consumed);
987 generator_.FinishBatchOperations();
988 EXPECT_FALSE(generator_.HasQueuedFrames());
989
990 EXPECT_LE(2u, packets_.size());
991 PacketContents contents;
992 // The first packet has two stream frames.
993 contents.num_stream_frames = 2;
994 CheckPacketContains(contents, 0);
995
996 // The second packet has one stream frame and padding frames.
997 contents.num_stream_frames = 1;
998 contents.num_padding_frames = 1;
999 CheckPacketContains(contents, 1);
1000
1001 for (size_t i = 2; i < packets_.size(); ++i) {
1002 // Following packets only have paddings.
1003 contents.num_stream_frames = 0;
1004 contents.num_padding_frames = 1;
1005 CheckPacketContains(contents, i);
1006 }
1007 }
1008
865 } // namespace test 1009 } // namespace test
866 } // namespace net 1010 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/core/quic_packet_generator.cc ('k') | net/quic/core/quic_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698