blob: d0cfdaef978079f76f77c5f2a41adf0c615f7964 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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_creator.h"
6
7#include <cstdint>
8#include <memory>
9#include <ostream>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
zhongyi546cc452019-04-12 15:27:49 -070012#include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#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_data_writer.h"
17#include "net/third_party/quiche/src/quic/core/quic_pending_retransmission.h"
18#include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h"
19#include "net/third_party/quiche/src/quic/core/quic_types.h"
20#include "net/third_party/quiche/src/quic/core/quic_utils.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050023#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
25#include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h"
26#include "net/third_party/quiche/src/quic/test_tools/quic_packet_creator_peer.h"
27#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
28#include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h"
29
30using testing::_;
31using testing::DoAll;
32using testing::InSequence;
33using testing::Invoke;
34using testing::Return;
35using testing::SaveArg;
36using testing::StrictMock;
37
38namespace quic {
39namespace test {
40namespace {
41
42// Run tests with combinations of {ParsedQuicVersion,
43// ToggleVersionSerialization}.
44struct TestParams {
45 TestParams(ParsedQuicVersion version, bool version_serialization)
46 : version(version), version_serialization(version_serialization) {}
47
48 friend std::ostream& operator<<(std::ostream& os, const TestParams& p) {
49 os << "{ version: " << ParsedQuicVersionToString(p.version)
50 << " include version: " << p.version_serialization << " }";
51 return os;
52 }
53
54 ParsedQuicVersion version;
55 bool version_serialization;
56};
57
58// Constructs various test permutations.
59std::vector<TestParams> GetTestParams() {
60 std::vector<TestParams> params;
61 ParsedQuicVersionVector all_supported_versions = AllSupportedVersions();
62 for (size_t i = 0; i < all_supported_versions.size(); ++i) {
63 params.push_back(TestParams(all_supported_versions[i], true));
64 params.push_back(TestParams(all_supported_versions[i], false));
65 }
66 return params;
67}
68
rchc76cd742019-03-26 16:00:03 -070069class MockDebugDelegate : public QuicPacketCreator::DebugDelegate {
70 public:
71 ~MockDebugDelegate() override = default;
72
73 MOCK_METHOD1(OnFrameAddedToPacket, void(const QuicFrame& frame));
74};
75
QUICHE teama6ef0a62019-03-07 20:34:33 -050076class TestPacketCreator : public QuicPacketCreator {
77 public:
78 TestPacketCreator(QuicConnectionId connection_id,
79 QuicFramer* framer,
80 DelegateInterface* delegate,
81 SimpleDataProducer* producer)
82 : QuicPacketCreator(connection_id, framer, delegate),
83 producer_(producer),
84 version_(framer->transport_version()) {}
85
86 bool ConsumeData(QuicStreamId id,
87 const struct iovec* iov,
88 int iov_count,
89 size_t total_length,
90 size_t iov_offset,
91 QuicStreamOffset offset,
92 bool fin,
93 bool needs_full_padding,
94 TransmissionType transmission_type,
95 QuicFrame* frame) {
96 // Save data before data is consumed.
97 QuicByteCount data_length = total_length - iov_offset;
98 if (data_length > 0) {
99 producer_->SaveStreamData(id, iov, iov_count, iov_offset, data_length);
100 }
ianswette28f0222019-04-04 13:31:22 -0700101 return QuicPacketCreator::ConsumeData(id, data_length - iov_offset, offset,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102 fin, needs_full_padding,
103 transmission_type, frame);
104 }
105
106 void StopSendingVersion() {
fayangd4291e42019-05-30 10:31:21 -0700107 if (VersionHasIetfInvariantHeader(version_)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108 set_encryption_level(ENCRYPTION_FORWARD_SECURE);
109 return;
110 }
111 QuicPacketCreator::StopSendingVersion();
112 }
113
114 SimpleDataProducer* producer_;
115 QuicTransportVersion version_;
116};
117
118class QuicPacketCreatorTest : public QuicTestWithParam<TestParams> {
119 public:
120 void ClearSerializedPacketForTests(SerializedPacket* serialized_packet) {
121 if (serialized_packet == nullptr) {
122 return;
123 }
124 ClearSerializedPacket(serialized_packet);
125 }
126
127 void SaveSerializedPacket(SerializedPacket* serialized_packet) {
128 if (serialized_packet == nullptr) {
129 return;
130 }
131 delete[] serialized_packet_.encrypted_buffer;
132 serialized_packet_ = *serialized_packet;
133 serialized_packet_.encrypted_buffer = CopyBuffer(*serialized_packet);
134 serialized_packet->retransmittable_frames.clear();
135 }
136
137 void DeleteSerializedPacket() {
138 delete[] serialized_packet_.encrypted_buffer;
139 serialized_packet_.encrypted_buffer = nullptr;
140 ClearSerializedPacket(&serialized_packet_);
141 }
142
143 protected:
144 QuicPacketCreatorTest()
145 : connection_id_(TestConnectionId(2)),
146 server_framer_(SupportedVersions(GetParam().version),
147 QuicTime::Zero(),
148 Perspective::IS_SERVER,
149 connection_id_.length()),
150 client_framer_(SupportedVersions(GetParam().version),
151 QuicTime::Zero(),
152 Perspective::IS_CLIENT,
153 connection_id_.length()),
154 data_("foo"),
155 creator_(connection_id_, &client_framer_, &delegate_, &producer_),
156 serialized_packet_(creator_.NoPacket()) {
157 EXPECT_CALL(delegate_, GetPacketBuffer()).WillRepeatedly(Return(nullptr));
vasilvv0fc587f2019-09-06 13:33:08 -0700158 creator_.SetEncrypter(ENCRYPTION_HANDSHAKE, std::make_unique<NullEncrypter>(
QUICHE team88ea0082019-03-15 10:05:26 -0700159 Perspective::IS_CLIENT));
vasilvv0fc587f2019-09-06 13:33:08 -0700160 creator_.SetEncrypter(ENCRYPTION_ZERO_RTT, std::make_unique<NullEncrypter>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161 Perspective::IS_CLIENT));
162 creator_.SetEncrypter(
163 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -0700164 std::make_unique<NullEncrypter>(Perspective::IS_CLIENT));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165 client_framer_.set_visitor(&framer_visitor_);
166 server_framer_.set_visitor(&framer_visitor_);
167 client_framer_.set_data_producer(&producer_);
zhongyi546cc452019-04-12 15:27:49 -0700168 if (server_framer_.version().KnowsWhichDecrypterToUse()) {
169 server_framer_.InstallDecrypter(
170 ENCRYPTION_ZERO_RTT,
vasilvv0fc587f2019-09-06 13:33:08 -0700171 std::make_unique<NullDecrypter>(Perspective::IS_SERVER));
zhongyi546cc452019-04-12 15:27:49 -0700172 server_framer_.InstallDecrypter(
173 ENCRYPTION_HANDSHAKE,
vasilvv0fc587f2019-09-06 13:33:08 -0700174 std::make_unique<NullDecrypter>(Perspective::IS_SERVER));
zhongyi546cc452019-04-12 15:27:49 -0700175 server_framer_.InstallDecrypter(
176 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -0700177 std::make_unique<NullDecrypter>(Perspective::IS_SERVER));
zhongyi546cc452019-04-12 15:27:49 -0700178 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500179 }
180
181 ~QuicPacketCreatorTest() override {
182 delete[] serialized_packet_.encrypted_buffer;
183 ClearSerializedPacket(&serialized_packet_);
184 }
185
186 SerializedPacket SerializeAllFrames(const QuicFrames& frames) {
187 SerializedPacket packet = QuicPacketCreatorPeer::SerializeAllFrames(
dschinazi66dea072019-04-09 11:41:06 -0700188 &creator_, frames, buffer_, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500189 EXPECT_EQ(QuicPacketCreatorPeer::GetEncryptionLevel(&creator_),
190 packet.encryption_level);
191 return packet;
192 }
193
194 void ProcessPacket(const SerializedPacket& packet) {
195 QuicEncryptedPacket encrypted_packet(packet.encrypted_buffer,
196 packet.encrypted_length);
197 server_framer_.ProcessPacket(encrypted_packet);
198 }
199
200 void CheckStreamFrame(const QuicFrame& frame,
201 QuicStreamId stream_id,
vasilvvc48c8712019-03-11 13:38:16 -0700202 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500203 QuicStreamOffset offset,
204 bool fin) {
205 EXPECT_EQ(STREAM_FRAME, frame.type);
206 EXPECT_EQ(stream_id, frame.stream_frame.stream_id);
dschinazi66dea072019-04-09 11:41:06 -0700207 char buf[kMaxOutgoingPacketSize];
208 QuicDataWriter writer(kMaxOutgoingPacketSize, buf, HOST_BYTE_ORDER);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500209 if (frame.stream_frame.data_length > 0) {
210 producer_.WriteStreamData(stream_id, frame.stream_frame.offset,
211 frame.stream_frame.data_length, &writer);
212 }
213 EXPECT_EQ(data, QuicStringPiece(buf, frame.stream_frame.data_length));
214 EXPECT_EQ(offset, frame.stream_frame.offset);
215 EXPECT_EQ(fin, frame.stream_frame.fin);
216 }
217
218 // Returns the number of bytes consumed by the header of packet, including
219 // the version.
220 size_t GetPacketHeaderOverhead(QuicTransportVersion version) {
221 return GetPacketHeaderSize(
222 version, creator_.GetDestinationConnectionIdLength(),
223 creator_.GetSourceConnectionIdLength(),
224 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
225 !kIncludeDiversificationNonce,
226 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_),
227 QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_), 0,
228 QuicPacketCreatorPeer::GetLengthLength(&creator_));
229 }
230
231 // Returns the number of bytes of overhead that will be added to a packet
232 // of maximum length.
233 size_t GetEncryptionOverhead() {
234 return creator_.max_packet_length() -
235 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
236 }
237
238 // Returns the number of bytes consumed by the non-data fields of a stream
239 // frame, assuming it is the last frame in the packet
240 size_t GetStreamFrameOverhead(QuicTransportVersion version) {
241 return QuicFramer::GetMinStreamFrameSize(
242 version, GetNthClientInitiatedStreamId(1), kOffset, true,
243 /* data_length= */ 0);
244 }
245
246 QuicPendingRetransmission CreateRetransmission(
247 const QuicFrames& retransmittable_frames,
248 bool has_crypto_handshake,
249 int num_padding_bytes,
250 EncryptionLevel encryption_level,
251 QuicPacketNumberLength packet_number_length) {
252 return QuicPendingRetransmission(QuicPacketNumber(1u), NOT_RETRANSMISSION,
253 retransmittable_frames,
254 has_crypto_handshake, num_padding_bytes,
255 encryption_level, packet_number_length);
256 }
257
258 bool IsDefaultTestConfiguration() {
259 TestParams p = GetParam();
260 return p.version == AllSupportedVersions()[0] && p.version_serialization;
261 }
262
263 QuicStreamId GetNthClientInitiatedStreamId(int n) const {
dschinazi552accc2019-06-17 17:07:34 -0700264 return QuicUtils::GetFirstBidirectionalStreamId(
265 creator_.transport_version(), Perspective::IS_CLIENT) +
266 n * 2;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500267 }
268
269 static const QuicStreamOffset kOffset = 0u;
270
dschinazi66dea072019-04-09 11:41:06 -0700271 char buffer_[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 QuicConnectionId connection_id_;
273 QuicFrames frames_;
274 QuicFramer server_framer_;
275 QuicFramer client_framer_;
276 StrictMock<MockFramerVisitor> framer_visitor_;
277 StrictMock<MockPacketCreatorDelegate> delegate_;
vasilvvc48c8712019-03-11 13:38:16 -0700278 std::string data_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500279 struct iovec iov_;
280 TestPacketCreator creator_;
281 SerializedPacket serialized_packet_;
282 SimpleDataProducer producer_;
283 SimpleBufferAllocator allocator_;
284};
285
286// Run all packet creator tests with all supported versions of QUIC, and with
287// and without version in the packet header, as well as doing a run for each
288// length of truncated connection id.
289INSTANTIATE_TEST_SUITE_P(QuicPacketCreatorTests,
290 QuicPacketCreatorTest,
291 ::testing::ValuesIn(GetTestParams()));
292
293TEST_P(QuicPacketCreatorTest, SerializeFrames) {
QUICHE team6987b4a2019-03-15 16:23:04 -0700294 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500295 EncryptionLevel level = static_cast<EncryptionLevel>(i);
296 creator_.set_encryption_level(level);
297 frames_.push_back(QuicFrame(new QuicAckFrame(InitAckFrame(1))));
QUICHE teamdc41bf12019-03-20 12:58:42 -0700298 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
299 client_framer_.transport_version(), Perspective::IS_CLIENT);
fayang49523232019-05-03 06:28:22 -0700300 if (level != ENCRYPTION_INITIAL && level != ENCRYPTION_HANDSHAKE) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700301 frames_.push_back(
302 QuicFrame(QuicStreamFrame(stream_id, false, 0u, QuicStringPiece())));
303 frames_.push_back(
304 QuicFrame(QuicStreamFrame(stream_id, true, 0u, QuicStringPiece())));
305 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500306 SerializedPacket serialized = SerializeAllFrames(frames_);
307 EXPECT_EQ(level, serialized.encryption_level);
308 delete frames_[0].ack_frame;
309 frames_.clear();
310
311 {
312 InSequence s;
313 EXPECT_CALL(framer_visitor_, OnPacket());
314 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
315 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
316 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
317 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
318 EXPECT_CALL(framer_visitor_, OnAckFrameStart(_, _))
319 .WillOnce(Return(true));
320 EXPECT_CALL(framer_visitor_,
321 OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2)))
322 .WillOnce(Return(true));
323 EXPECT_CALL(framer_visitor_, OnAckFrameEnd(QuicPacketNumber(1)))
324 .WillOnce(Return(true));
fayang49523232019-05-03 06:28:22 -0700325 if (level != ENCRYPTION_INITIAL && level != ENCRYPTION_HANDSHAKE) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700326 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
327 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
328 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500329 EXPECT_CALL(framer_visitor_, OnPacketComplete());
330 }
331 ProcessPacket(serialized);
332 }
333}
334
335TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
fayangd4291e42019-05-30 10:31:21 -0700336 if (VersionHasIetfInvariantHeader(client_framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500337 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
338 }
339 // If the original packet number length, the current packet number
340 // length, and the configured send packet number length are different, the
341 // retransmit must sent with the original length and the others do not change.
342 QuicPacketCreatorPeer::SetPacketNumberLength(&creator_,
343 PACKET_2BYTE_PACKET_NUMBER);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500344 QuicFrames frames;
QUICHE teamdc41bf12019-03-20 12:58:42 -0700345 std::string data("a");
346 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
347 QuicStreamFrame stream_frame(
348 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
349 /*fin=*/false, 0u, QuicStringPiece());
350 frames.push_back(QuicFrame(stream_frame));
351 } else {
352 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
353 frames.push_back(
354 QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length())));
355 }
dschinazi66dea072019-04-09 11:41:06 -0700356 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500357 QuicPendingRetransmission retransmission(CreateRetransmission(
358 frames, true /* has_crypto_handshake */, -1 /* needs full padding */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700359 ENCRYPTION_INITIAL, PACKET_4BYTE_PACKET_NUMBER));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500360 EXPECT_CALL(delegate_, OnSerializedPacket(_))
361 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700362 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500363 // The packet number length is updated after every packet is sent,
364 // so there is no need to restore the old length after sending.
365 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
366 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
367 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
368 serialized_packet_.packet_number_length);
369
370 {
371 InSequence s;
372 EXPECT_CALL(framer_visitor_, OnPacket());
373 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
374 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
375 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
376 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
QUICHE teamdc41bf12019-03-20 12:58:42 -0700377 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
378 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
379 } else {
380 EXPECT_CALL(framer_visitor_, OnCryptoFrame(_));
381 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500382 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
383 EXPECT_CALL(framer_visitor_, OnPacketComplete());
384 }
385 ProcessPacket(serialized_packet_);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700386 DeleteFrames(&frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500387}
388
389TEST_P(QuicPacketCreatorTest, ReserializeCryptoFrameWithForwardSecurity) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500390 QuicFrames frames;
QUICHE teamdc41bf12019-03-20 12:58:42 -0700391 std::string data("a");
392 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
393 QuicStreamFrame stream_frame(
394 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
395 /*fin=*/false, 0u, QuicStringPiece());
396 frames.push_back(QuicFrame(stream_frame));
397 } else {
398 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
399 frames.push_back(
400 QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length())));
401 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500402 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
dschinazi66dea072019-04-09 11:41:06 -0700403 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500404 QuicPendingRetransmission retransmission(CreateRetransmission(
405 frames, true /* has_crypto_handshake */, -1 /* needs full padding */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700406 ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500407 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
408 EXPECT_CALL(delegate_, OnSerializedPacket(_))
409 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700410 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE team6987b4a2019-03-15 16:23:04 -0700411 EXPECT_EQ(ENCRYPTION_INITIAL, serialized_packet_.encryption_level);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700412 DeleteFrames(&frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500413}
414
415TEST_P(QuicPacketCreatorTest, ReserializeFrameWithForwardSecurity) {
416 QuicStreamFrame stream_frame(0u, /*fin=*/false, 0u, QuicStringPiece());
417 QuicFrames frames;
418 frames.push_back(QuicFrame(stream_frame));
419 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
dschinazi66dea072019-04-09 11:41:06 -0700420 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500421 QuicPendingRetransmission retransmission(CreateRetransmission(
422 frames, false /* has_crypto_handshake */, 0 /* no padding */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700423 ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500424 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
425 EXPECT_CALL(delegate_, OnSerializedPacket(_))
426 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700427 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500428 EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, serialized_packet_.encryption_level);
429}
430
431TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPadding) {
432 QuicFrame frame;
QUICHE teamdc41bf12019-03-20 12:58:42 -0700433 std::string data = "fake handshake message data";
434 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
435 MakeIOVector(data, &iov_);
436 producer_.SaveStreamData(
437 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), &iov_,
438 1u, 0u, iov_.iov_len);
439 QuicPacketCreatorPeer::CreateStreamFrame(
440 &creator_,
441 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
442 iov_.iov_len, 0u, false, &frame);
443 } else {
444 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
445 EXPECT_TRUE(QuicPacketCreatorPeer::CreateCryptoFrame(
446 &creator_, ENCRYPTION_INITIAL, data.length(), 0, &frame));
447 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500448 QuicFrames frames;
449 frames.push_back(frame);
dschinazi66dea072019-04-09 11:41:06 -0700450 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500451 QuicPendingRetransmission retransmission(CreateRetransmission(
452 frames, true /* has_crypto_handshake */, -1 /* needs full padding */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700453 ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500454 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
455 EXPECT_CALL(delegate_, OnSerializedPacket(_))
456 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700457 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500458 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700459 DeleteFrames(&frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500460}
461
462TEST_P(QuicPacketCreatorTest, DoNotRetransmitPendingPadding) {
463 QuicFrame frame;
QUICHE teamdc41bf12019-03-20 12:58:42 -0700464 std::string data = "fake message data";
465 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
466 MakeIOVector(data, &iov_);
467 producer_.SaveStreamData(
468 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), &iov_,
469 1u, 0u, iov_.iov_len);
470 QuicPacketCreatorPeer::CreateStreamFrame(
471 &creator_,
472 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
473 iov_.iov_len, 0u, false, &frame);
474 } else {
475 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
476 EXPECT_TRUE(QuicPacketCreatorPeer::CreateCryptoFrame(
477 &creator_, ENCRYPTION_INITIAL, data.length(), 0, &frame));
478 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500479
480 const int kNumPaddingBytes1 = 4;
481 int packet_size = 0;
482 {
483 QuicFrames frames;
484 frames.push_back(frame);
dschinazi66dea072019-04-09 11:41:06 -0700485 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500486 QuicPendingRetransmission retransmission(CreateRetransmission(
487 frames, false /* has_crypto_handshake */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700488 kNumPaddingBytes1 /* padding bytes */, ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500489 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
490 EXPECT_CALL(delegate_, OnSerializedPacket(_))
491 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700492 creator_.ReserializeAllFrames(retransmission, buffer,
493 kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500494 packet_size = serialized_packet_.encrypted_length;
495 }
496
497 {
498 InSequence s;
499 EXPECT_CALL(framer_visitor_, OnPacket());
500 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
501 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
502 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
503 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
QUICHE teamdc41bf12019-03-20 12:58:42 -0700504 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
505 EXPECT_CALL(framer_visitor_, OnCryptoFrame(_));
506 } else {
507 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
508 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500509 // Pending paddings are not retransmitted.
510 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)).Times(0);
511 EXPECT_CALL(framer_visitor_, OnPacketComplete());
512 }
513 ProcessPacket(serialized_packet_);
514
515 const int kNumPaddingBytes2 = 44;
516 QuicFrames frames;
517 frames.push_back(frame);
dschinazi66dea072019-04-09 11:41:06 -0700518 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500519 QuicPendingRetransmission retransmission(CreateRetransmission(
520 frames, false /* has_crypto_handshake */,
QUICHE team6987b4a2019-03-15 16:23:04 -0700521 kNumPaddingBytes2 /* padding bytes */, ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500522 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
523 EXPECT_CALL(delegate_, OnSerializedPacket(_))
524 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700525 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500526
527 EXPECT_EQ(packet_size, serialized_packet_.encrypted_length);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700528 DeleteFrames(&frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500529}
530
531TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700532 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500533 const size_t overhead =
534 GetPacketHeaderOverhead(client_framer_.transport_version()) +
535 GetEncryptionOverhead() +
536 GetStreamFrameOverhead(client_framer_.transport_version());
537 size_t capacity = kDefaultMaxPacketSize - overhead;
538 for (int delta = -5; delta <= 0; ++delta) {
vasilvvc48c8712019-03-11 13:38:16 -0700539 std::string data(capacity + delta, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500540 size_t bytes_free = 0 - delta;
541
542 QuicFrame frame;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500543 SimpleDataProducer producer;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500544 QuicPacketCreatorPeer::framer(&creator_)->set_data_producer(&producer);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700545 MakeIOVector(data, &iov_);
546 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
547 client_framer_.transport_version(), Perspective::IS_CLIENT);
548 producer.SaveStreamData(stream_id, &iov_, 1u, 0u, iov_.iov_len);
549 QuicPacketCreatorPeer::CreateStreamFrame(&creator_, stream_id, iov_.iov_len,
550 kOffset, false, &frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500551 QuicFrames frames;
552 frames.push_back(frame);
dschinazi66dea072019-04-09 11:41:06 -0700553 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500554 QuicPendingRetransmission retransmission(CreateRetransmission(
QUICHE teamdc41bf12019-03-20 12:58:42 -0700555 frames, false /* has_crypto_handshake */, -1 /* needs full padding */,
556 ENCRYPTION_FORWARD_SECURE,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500557 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
558 EXPECT_CALL(delegate_, OnSerializedPacket(_))
559 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -0700560 creator_.ReserializeAllFrames(retransmission, buffer,
561 kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500562
563 // If there is not enough space in the packet to fit a padding frame
564 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
565 // will not be padded.
566 if (bytes_free < 3) {
567 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
568 serialized_packet_.encrypted_length);
569 } else {
570 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length);
571 }
572
573 frames_.clear();
574 }
575}
576
577TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) {
fkastenholz591814c2019-09-06 12:11:46 -0700578 QuicConnectionCloseFrame frame(GetParam().version.transport_version,
579 QUIC_NO_ERROR, "error",
580 /*transport_close_frame_type=*/0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500581
582 QuicFrames frames;
583 frames.push_back(QuicFrame(&frame));
584 SerializedPacket serialized = SerializeAllFrames(frames);
QUICHE team6987b4a2019-03-15 16:23:04 -0700585 EXPECT_EQ(ENCRYPTION_INITIAL, serialized.encryption_level);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500586 ASSERT_EQ(QuicPacketNumber(1u), serialized.packet_number);
587 ASSERT_EQ(QuicPacketNumber(1u), creator_.packet_number());
588
589 InSequence s;
590 EXPECT_CALL(framer_visitor_, OnPacket());
591 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
592 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
593 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
594 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
595 EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
596 EXPECT_CALL(framer_visitor_, OnPacketComplete());
597
598 ProcessPacket(serialized);
599}
600
601TEST_P(QuicPacketCreatorTest, ConsumeCryptoData) {
vasilvvc48c8712019-03-11 13:38:16 -0700602 std::string data = "crypto data";
QUICHE teama6ef0a62019-03-07 20:34:33 -0500603 QuicFrame frame;
QUICHE team6987b4a2019-03-15 16:23:04 -0700604 ASSERT_TRUE(creator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data.length(), 0,
nharper51961cf2019-05-13 13:23:24 -0700605 /*needs_full_padding=*/true,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500606 NOT_RETRANSMISSION, &frame));
607 EXPECT_EQ(frame.crypto_frame->data_length, data.length());
608 EXPECT_TRUE(creator_.HasPendingFrames());
609}
610
611TEST_P(QuicPacketCreatorTest, ConsumeData) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700612 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500613 QuicFrame frame;
614 MakeIOVector("test", &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700615 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
616 client_framer_.transport_version(), Perspective::IS_CLIENT);
617 ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u,
618 false, false, NOT_RETRANSMISSION, &frame));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500619 size_t consumed = frame.stream_frame.data_length;
620 EXPECT_EQ(4u, consumed);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700621 CheckStreamFrame(frame, stream_id, "test", 0u, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500622 EXPECT_TRUE(creator_.HasPendingFrames());
623}
624
625TEST_P(QuicPacketCreatorTest, ConsumeDataFin) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700626 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500627 QuicFrame frame;
628 MakeIOVector("test", &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700629 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
630 client_framer_.transport_version(), Perspective::IS_CLIENT);
631 ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u,
632 true, false, NOT_RETRANSMISSION, &frame));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500633 size_t consumed = frame.stream_frame.data_length;
634 EXPECT_EQ(4u, consumed);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700635 CheckStreamFrame(frame, stream_id, "test", 0u, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500636 EXPECT_TRUE(creator_.HasPendingFrames());
637}
638
639TEST_P(QuicPacketCreatorTest, ConsumeDataFinOnly) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700640 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500641 QuicFrame frame;
QUICHE teamdc41bf12019-03-20 12:58:42 -0700642 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
643 client_framer_.transport_version(), Perspective::IS_CLIENT);
644 ASSERT_TRUE(creator_.ConsumeData(stream_id, nullptr, 0u, 0u, 0u, 0u, true,
645 false, NOT_RETRANSMISSION, &frame));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500646 size_t consumed = frame.stream_frame.data_length;
647 EXPECT_EQ(0u, consumed);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700648 CheckStreamFrame(frame, stream_id, std::string(), 0u, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500649 EXPECT_TRUE(creator_.HasPendingFrames());
650}
651
652TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
653 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
654 const size_t overhead =
655 GetPacketHeaderOverhead(client_framer_.transport_version()) +
656 GetEncryptionOverhead();
QUICHE team2252b702019-05-14 23:55:14 -0400657 for (size_t i = overhead + QuicPacketCreator::MinPlaintextPacketSize(
658 client_framer_.version());
nharper55fa6132019-05-07 19:37:21 -0700659 i < overhead + 100; ++i) {
660 SCOPED_TRACE(i);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500661 creator_.SetMaxPacketLength(i);
662 const bool should_have_room =
663 i >
664 overhead + GetStreamFrameOverhead(client_framer_.transport_version());
665 ASSERT_EQ(should_have_room,
666 creator_.HasRoomForStreamFrame(GetNthClientInitiatedStreamId(1),
667 kOffset, /* data_size=*/0xffff));
668 if (should_have_room) {
669 QuicFrame frame;
670 MakeIOVector("testdata", &iov_);
671 EXPECT_CALL(delegate_, OnSerializedPacket(_))
672 .WillRepeatedly(Invoke(
673 this, &QuicPacketCreatorTest::ClearSerializedPacketForTests));
674 ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_,
675 1u, iov_.iov_len, 0u, kOffset, false,
676 false, NOT_RETRANSMISSION, &frame));
677 size_t bytes_consumed = frame.stream_frame.data_length;
678 EXPECT_LT(0u, bytes_consumed);
679 creator_.Flush();
680 }
681 }
682}
683
684TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) {
685 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
686 // Compute the total overhead for a single frame in packet.
687 const size_t overhead =
688 GetPacketHeaderOverhead(client_framer_.transport_version()) +
689 GetEncryptionOverhead() +
690 GetStreamFrameOverhead(client_framer_.transport_version());
691 size_t capacity = kDefaultMaxPacketSize - overhead;
692 // Now, test various sizes around this size.
693 for (int delta = -5; delta <= 5; ++delta) {
vasilvvc48c8712019-03-11 13:38:16 -0700694 std::string data(capacity + delta, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500695 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
696 QuicFrame frame;
697 MakeIOVector(data, &iov_);
698 ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_,
699 1u, iov_.iov_len, 0u, kOffset, false,
700 false, NOT_RETRANSMISSION, &frame));
701
702 // BytesFree() returns bytes available for the next frame, which will
703 // be two bytes smaller since the stream frame would need to be grown.
704 EXPECT_EQ(2u, creator_.ExpansionOnNewFrame());
705 size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
706 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
707 EXPECT_CALL(delegate_, OnSerializedPacket(_))
708 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
709 creator_.Flush();
710 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
711 DeleteSerializedPacket();
712 }
713}
714
715TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) {
ianswette28f0222019-04-04 13:31:22 -0700716 // This test serializes crypto payloads slightly larger than a packet, which
717 // Causes the multi-packet ClientHello check to fail.
danzh88e3e052019-06-13 11:47:18 -0700718 SetQuicFlag(FLAGS_quic_enforce_single_packet_chlo, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500719 // Compute the total overhead for a single frame in packet.
QUICHE teamdc41bf12019-03-20 12:58:42 -0700720 size_t overhead =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500721 GetPacketHeaderOverhead(client_framer_.transport_version()) +
QUICHE teamdc41bf12019-03-20 12:58:42 -0700722 GetEncryptionOverhead();
723 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
dschinazi66dea072019-04-09 11:41:06 -0700724 overhead +=
725 QuicFramer::GetMinCryptoFrameSize(kOffset, kMaxOutgoingPacketSize);
QUICHE teamdc41bf12019-03-20 12:58:42 -0700726 } else {
727 overhead += GetStreamFrameOverhead(client_framer_.transport_version());
728 }
dschinazi66dea072019-04-09 11:41:06 -0700729 ASSERT_GT(kMaxOutgoingPacketSize, overhead);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500730 size_t capacity = kDefaultMaxPacketSize - overhead;
731 // Now, test various sizes around this size.
732 for (int delta = -5; delta <= 5; ++delta) {
QUICHE teamdc41bf12019-03-20 12:58:42 -0700733 SCOPED_TRACE(delta);
vasilvvc48c8712019-03-11 13:38:16 -0700734 std::string data(capacity + delta, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500735 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
736
737 QuicFrame frame;
738 MakeIOVector(data, &iov_);
739 EXPECT_CALL(delegate_, OnSerializedPacket(_))
740 .WillRepeatedly(
741 Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
QUICHE teamdc41bf12019-03-20 12:58:42 -0700742 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
743 ASSERT_TRUE(creator_.ConsumeData(
744 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
745 &iov_, 1u, iov_.iov_len, 0u, kOffset, false, true, NOT_RETRANSMISSION,
746 &frame));
747 size_t bytes_consumed = frame.stream_frame.data_length;
748 EXPECT_LT(0u, bytes_consumed);
749 } else {
750 producer_.SaveCryptoData(ENCRYPTION_INITIAL, kOffset, data);
nharper51961cf2019-05-13 13:23:24 -0700751 ASSERT_TRUE(creator_.ConsumeCryptoData(
752 ENCRYPTION_INITIAL, data.length(), kOffset,
753 /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame));
QUICHE teamdc41bf12019-03-20 12:58:42 -0700754 size_t bytes_consumed = frame.crypto_frame->data_length;
755 EXPECT_LT(0u, bytes_consumed);
756 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500757 creator_.Flush();
758 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
759 // If there is not enough space in the packet to fit a padding frame
760 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
761 // will not be padded.
QUICHE teamdc41bf12019-03-20 12:58:42 -0700762 if (bytes_free < 3 &&
763 !QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500764 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
765 serialized_packet_.encrypted_length);
766 } else {
767 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length);
768 }
769 DeleteSerializedPacket();
770 }
771}
772
773TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) {
774 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
775 // Compute the total overhead for a single frame in packet.
776 const size_t overhead =
777 GetPacketHeaderOverhead(client_framer_.transport_version()) +
778 GetEncryptionOverhead() +
779 GetStreamFrameOverhead(client_framer_.transport_version());
780 ASSERT_GT(kDefaultMaxPacketSize, overhead);
781 size_t capacity = kDefaultMaxPacketSize - overhead;
782 // Now, test various sizes around this size.
783 for (int delta = -5; delta <= 5; ++delta) {
vasilvvc48c8712019-03-11 13:38:16 -0700784 std::string data(capacity + delta, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500785 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
786
787 QuicFrame frame;
788 MakeIOVector(data, &iov_);
789 EXPECT_CALL(delegate_, OnSerializedPacket(_))
790 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
791 ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_,
792 1u, iov_.iov_len, 0u, kOffset, false,
793 false, NOT_RETRANSMISSION, &frame));
794 size_t bytes_consumed = frame.stream_frame.data_length;
795 EXPECT_LT(0u, bytes_consumed);
796 creator_.Flush();
797 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
798 if (bytes_free > 0) {
799 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
800 serialized_packet_.encrypted_length);
801 } else {
802 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length);
803 }
804 DeleteSerializedPacket();
805 }
806}
807
808TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) {
809 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
810 ParsedQuicVersionVector versions;
811 versions.push_back(test::QuicVersionMax());
fayangd4291e42019-05-30 10:31:21 -0700812 const bool ietf_quic =
813 VersionHasIetfInvariantHeader(GetParam().version.transport_version);
dschinazi48ac9192019-07-31 00:07:26 -0700814 const bool has_length_prefix =
815 GetParam().version.HasLengthPrefixedConnectionIds();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500816 std::unique_ptr<QuicEncryptedPacket> encrypted(
dschinazi48ac9192019-07-31 00:07:26 -0700817 creator_.SerializeVersionNegotiationPacket(ietf_quic, has_length_prefix,
818 versions));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500819
820 {
821 InSequence s;
822 EXPECT_CALL(framer_visitor_, OnPacket());
823 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
824 EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
825 }
826 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_CLIENT);
827 client_framer_.ProcessPacket(*encrypted);
828}
829
830TEST_P(QuicPacketCreatorTest, SerializeConnectivityProbingPacket) {
QUICHE team6987b4a2019-03-15 16:23:04 -0700831 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500832 EncryptionLevel level = static_cast<EncryptionLevel>(i);
833
834 creator_.set_encryption_level(level);
835
836 OwningSerializedPacketPointer encrypted;
fkastenholz305e1732019-06-18 05:01:22 -0700837 if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500838 QuicPathFrameBuffer payload = {
839 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}};
840 encrypted =
841 creator_.SerializePathChallengeConnectivityProbingPacket(&payload);
842 } else {
843 encrypted = creator_.SerializeConnectivityProbingPacket();
844 }
845 {
846 InSequence s;
847 EXPECT_CALL(framer_visitor_, OnPacket());
848 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
849 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
850 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
851 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
fkastenholz305e1732019-06-18 05:01:22 -0700852 if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500853 EXPECT_CALL(framer_visitor_, OnPathChallengeFrame(_));
854 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
855 } else {
856 EXPECT_CALL(framer_visitor_, OnPingFrame(_));
857 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
858 }
859 EXPECT_CALL(framer_visitor_, OnPacketComplete());
860 }
861 // QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
862 server_framer_.ProcessPacket(QuicEncryptedPacket(
863 encrypted->encrypted_buffer, encrypted->encrypted_length));
864 }
865}
866
867TEST_P(QuicPacketCreatorTest, SerializePathChallengeProbePacket) {
fkastenholz305e1732019-06-18 05:01:22 -0700868 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500869 return;
870 }
871 QuicPathFrameBuffer payload = {
872 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
873
QUICHE team6987b4a2019-03-15 16:23:04 -0700874 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500875 EncryptionLevel level = static_cast<EncryptionLevel>(i);
876
877 creator_.set_encryption_level(level);
878
879 OwningSerializedPacketPointer encrypted(
880 creator_.SerializePathChallengeConnectivityProbingPacket(&payload));
881 {
882 InSequence s;
883 EXPECT_CALL(framer_visitor_, OnPacket());
884 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
885 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
886 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
887 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
888 EXPECT_CALL(framer_visitor_, OnPathChallengeFrame(_));
889 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
890 EXPECT_CALL(framer_visitor_, OnPacketComplete());
891 }
892 // QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
893 server_framer_.ProcessPacket(QuicEncryptedPacket(
894 encrypted->encrypted_buffer, encrypted->encrypted_length));
895 }
896}
897
898TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket1PayloadPadded) {
fkastenholz305e1732019-06-18 05:01:22 -0700899 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500900 return;
901 }
902 QuicPathFrameBuffer payload0 = {
903 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
904
QUICHE team6987b4a2019-03-15 16:23:04 -0700905 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500906 EncryptionLevel level = static_cast<EncryptionLevel>(i);
907 creator_.set_encryption_level(level);
908
909 QuicDeque<QuicPathFrameBuffer> payloads;
910 payloads.push_back(payload0);
911
912 OwningSerializedPacketPointer encrypted(
913 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
914 true));
915 {
916 InSequence s;
917 EXPECT_CALL(framer_visitor_, OnPacket());
918 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
919 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
920 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
921 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
922 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_));
923 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
924 EXPECT_CALL(framer_visitor_, OnPacketComplete());
925 }
926 server_framer_.ProcessPacket(QuicEncryptedPacket(
927 encrypted->encrypted_buffer, encrypted->encrypted_length));
928 }
929}
930
931TEST_P(QuicPacketCreatorTest,
932 SerializePathResponseProbePacket1PayloadUnPadded) {
fkastenholz305e1732019-06-18 05:01:22 -0700933 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500934 return;
935 }
936 QuicPathFrameBuffer payload0 = {
937 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
938
QUICHE team6987b4a2019-03-15 16:23:04 -0700939 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500940 EncryptionLevel level = static_cast<EncryptionLevel>(i);
941 creator_.set_encryption_level(level);
942
943 QuicDeque<QuicPathFrameBuffer> payloads;
944 payloads.push_back(payload0);
945
946 OwningSerializedPacketPointer encrypted(
947 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
948 false));
949 {
950 InSequence s;
951 EXPECT_CALL(framer_visitor_, OnPacket());
952 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
953 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
954 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
955 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
956 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_));
957 EXPECT_CALL(framer_visitor_, OnPacketComplete());
958 }
959 server_framer_.ProcessPacket(QuicEncryptedPacket(
960 encrypted->encrypted_buffer, encrypted->encrypted_length));
961 }
962}
963
964TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket2PayloadsPadded) {
fkastenholz305e1732019-06-18 05:01:22 -0700965 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500966 return;
967 }
968 QuicPathFrameBuffer payload0 = {
969 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
970 QuicPathFrameBuffer payload1 = {
971 {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}};
972
QUICHE team6987b4a2019-03-15 16:23:04 -0700973 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500974 EncryptionLevel level = static_cast<EncryptionLevel>(i);
975 creator_.set_encryption_level(level);
976
977 QuicDeque<QuicPathFrameBuffer> payloads;
978 payloads.push_back(payload0);
979 payloads.push_back(payload1);
980
981 OwningSerializedPacketPointer encrypted(
982 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
983 true));
984 {
985 InSequence s;
986 EXPECT_CALL(framer_visitor_, OnPacket());
987 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
988 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
989 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
990 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
991 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(2);
992 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
993 EXPECT_CALL(framer_visitor_, OnPacketComplete());
994 }
995 server_framer_.ProcessPacket(QuicEncryptedPacket(
996 encrypted->encrypted_buffer, encrypted->encrypted_length));
997 }
998}
999
1000TEST_P(QuicPacketCreatorTest,
1001 SerializePathResponseProbePacket2PayloadsUnPadded) {
fkastenholz305e1732019-06-18 05:01:22 -07001002 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001003 return;
1004 }
1005 QuicPathFrameBuffer payload0 = {
1006 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
1007 QuicPathFrameBuffer payload1 = {
1008 {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}};
1009
QUICHE team6987b4a2019-03-15 16:23:04 -07001010 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001011 EncryptionLevel level = static_cast<EncryptionLevel>(i);
1012 creator_.set_encryption_level(level);
1013
1014 QuicDeque<QuicPathFrameBuffer> payloads;
1015 payloads.push_back(payload0);
1016 payloads.push_back(payload1);
1017
1018 OwningSerializedPacketPointer encrypted(
1019 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
1020 false));
1021 {
1022 InSequence s;
1023 EXPECT_CALL(framer_visitor_, OnPacket());
1024 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1025 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1026 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1027 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1028 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(2);
1029 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1030 }
1031 server_framer_.ProcessPacket(QuicEncryptedPacket(
1032 encrypted->encrypted_buffer, encrypted->encrypted_length));
1033 }
1034}
1035
1036TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket3PayloadsPadded) {
fkastenholz305e1732019-06-18 05:01:22 -07001037 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001038 return;
1039 }
1040 QuicPathFrameBuffer payload0 = {
1041 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
1042 QuicPathFrameBuffer payload1 = {
1043 {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}};
1044 QuicPathFrameBuffer payload2 = {
1045 {0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde, 0xad}};
1046
QUICHE team6987b4a2019-03-15 16:23:04 -07001047 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001048 EncryptionLevel level = static_cast<EncryptionLevel>(i);
1049 creator_.set_encryption_level(level);
1050
1051 QuicDeque<QuicPathFrameBuffer> payloads;
1052 payloads.push_back(payload0);
1053 payloads.push_back(payload1);
1054 payloads.push_back(payload2);
1055
1056 OwningSerializedPacketPointer encrypted(
1057 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
1058 true));
1059 {
1060 InSequence s;
1061 EXPECT_CALL(framer_visitor_, OnPacket());
1062 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1063 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1064 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1065 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1066 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(3);
1067 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1068 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1069 }
1070 server_framer_.ProcessPacket(QuicEncryptedPacket(
1071 encrypted->encrypted_buffer, encrypted->encrypted_length));
1072 }
1073}
1074
1075TEST_P(QuicPacketCreatorTest,
1076 SerializePathResponseProbePacket3PayloadsUnpadded) {
fkastenholz305e1732019-06-18 05:01:22 -07001077 if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001078 return;
1079 }
1080 QuicPathFrameBuffer payload0 = {
1081 {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}};
1082 QuicPathFrameBuffer payload1 = {
1083 {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}};
1084 QuicPathFrameBuffer payload2 = {
1085 {0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde, 0xad}};
1086
QUICHE team6987b4a2019-03-15 16:23:04 -07001087 for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001088 EncryptionLevel level = static_cast<EncryptionLevel>(i);
1089 creator_.set_encryption_level(level);
1090
1091 QuicDeque<QuicPathFrameBuffer> payloads;
1092 payloads.push_back(payload0);
1093 payloads.push_back(payload1);
1094 payloads.push_back(payload2);
1095
1096 OwningSerializedPacketPointer encrypted(
1097 creator_.SerializePathResponseConnectivityProbingPacket(payloads,
1098 false));
1099 InSequence s;
1100 EXPECT_CALL(framer_visitor_, OnPacket());
1101 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1102 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1103 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1104 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1105 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(3);
1106 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1107
1108 server_framer_.ProcessPacket(QuicEncryptedPacket(
1109 encrypted->encrypted_buffer, encrypted->encrypted_length));
1110 }
1111}
1112
1113TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
fayangd4291e42019-05-30 10:31:21 -07001114 if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) &&
fayang3ac15c12019-06-14 14:04:51 -07001115 !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001116 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1117 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1118 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
1119 } else {
1120 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1121 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1122 }
1123
1124 QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64);
1125 creator_.UpdatePacketNumberLength(QuicPacketNumber(2),
1126 10000 / kDefaultMaxPacketSize);
1127 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1128 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1129
1130 QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64 * 256);
1131 creator_.UpdatePacketNumberLength(QuicPacketNumber(2),
1132 10000 / kDefaultMaxPacketSize);
1133 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
1134 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1135
1136 QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64 * 256 * 256);
1137 creator_.UpdatePacketNumberLength(QuicPacketNumber(2),
1138 10000 / kDefaultMaxPacketSize);
1139 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1140 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1141
1142 QuicPacketCreatorPeer::SetPacketNumber(&creator_,
1143 UINT64_C(64) * 256 * 256 * 256 * 256);
1144 creator_.UpdatePacketNumberLength(QuicPacketNumber(2),
1145 10000 / kDefaultMaxPacketSize);
1146 EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER,
1147 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1148}
1149
1150TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthCwnd) {
1151 QuicPacketCreatorPeer::SetPacketNumber(&creator_, 1);
fayangd4291e42019-05-30 10:31:21 -07001152 if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) &&
fayang3ac15c12019-06-14 14:04:51 -07001153 !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001154 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1155 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1156 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
1157 } else {
1158 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1159 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1160 }
1161
1162 creator_.UpdatePacketNumberLength(QuicPacketNumber(1),
1163 10000 / kDefaultMaxPacketSize);
1164 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1165 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1166
1167 creator_.UpdatePacketNumberLength(QuicPacketNumber(1),
1168 10000 * 256 / kDefaultMaxPacketSize);
1169 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
1170 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1171
1172 creator_.UpdatePacketNumberLength(QuicPacketNumber(1),
1173 10000 * 256 * 256 / kDefaultMaxPacketSize);
1174 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1175 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1176
1177 creator_.UpdatePacketNumberLength(
1178 QuicPacketNumber(1),
1179 UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
1180 EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER,
1181 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1182}
1183
fayang4c1c2362019-09-13 07:20:01 -07001184TEST_P(QuicPacketCreatorTest, SkipNPacketNumbers) {
1185 QuicPacketCreatorPeer::SetPacketNumber(&creator_, 1);
1186 if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) &&
1187 !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) {
1188 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1189 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1190 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
1191 } else {
1192 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1193 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1194 }
1195 creator_.SkipNPacketNumbers(63, QuicPacketNumber(2),
1196 10000 / kDefaultMaxPacketSize);
1197 EXPECT_EQ(QuicPacketNumber(64), creator_.packet_number());
1198 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
1199 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1200
1201 creator_.SkipNPacketNumbers(64 * 255, QuicPacketNumber(2),
1202 10000 / kDefaultMaxPacketSize);
1203 EXPECT_EQ(QuicPacketNumber(64 * 256), creator_.packet_number());
1204 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
1205 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1206
1207 creator_.SkipNPacketNumbers(64 * 256 * 255, QuicPacketNumber(2),
1208 10000 / kDefaultMaxPacketSize);
1209 EXPECT_EQ(QuicPacketNumber(64 * 256 * 256), creator_.packet_number());
1210 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
1211 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
1212}
1213
QUICHE teama6ef0a62019-03-07 20:34:33 -05001214TEST_P(QuicPacketCreatorTest, SerializeFrame) {
1215 if (!GetParam().version_serialization) {
1216 creator_.StopSendingVersion();
1217 }
nharper55fa6132019-05-07 19:37:21 -07001218 std::string data("test data");
1219 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1220 QuicStreamFrame stream_frame(
1221 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
1222 /*fin=*/false, 0u, QuicStringPiece());
1223 frames_.push_back(QuicFrame(stream_frame));
1224 } else {
1225 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
1226 frames_.push_back(
1227 QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length())));
1228 }
1229 SerializedPacket serialized = SerializeAllFrames(frames_);
1230
1231 QuicPacketHeader header;
1232 {
1233 InSequence s;
1234 EXPECT_CALL(framer_visitor_, OnPacket());
1235 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1236 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1237 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1238 EXPECT_CALL(framer_visitor_, OnPacketHeader(_))
1239 .WillOnce(DoAll(SaveArg<0>(&header), Return(true)));
1240 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1241 EXPECT_CALL(framer_visitor_, OnCryptoFrame(_));
1242 } else {
1243 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1244 }
1245 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1246 }
1247 ProcessPacket(serialized);
1248 EXPECT_EQ(GetParam().version_serialization, header.version_flag);
1249 DeleteFrames(&frames_);
1250}
1251
1252TEST_P(QuicPacketCreatorTest, SerializeFrameShortData) {
1253 if (!GetParam().version_serialization) {
1254 creator_.StopSendingVersion();
1255 }
QUICHE teamdc41bf12019-03-20 12:58:42 -07001256 std::string data("a");
1257 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1258 QuicStreamFrame stream_frame(
1259 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
1260 /*fin=*/false, 0u, QuicStringPiece());
1261 frames_.push_back(QuicFrame(stream_frame));
1262 } else {
1263 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
1264 frames_.push_back(
1265 QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length())));
1266 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001267 SerializedPacket serialized = SerializeAllFrames(frames_);
1268
1269 QuicPacketHeader header;
1270 {
1271 InSequence s;
1272 EXPECT_CALL(framer_visitor_, OnPacket());
1273 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1274 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1275 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1276 EXPECT_CALL(framer_visitor_, OnPacketHeader(_))
1277 .WillOnce(DoAll(SaveArg<0>(&header), Return(true)));
QUICHE teamdc41bf12019-03-20 12:58:42 -07001278 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1279 EXPECT_CALL(framer_visitor_, OnCryptoFrame(_));
1280 } else {
1281 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1282 }
nharper55fa6132019-05-07 19:37:21 -07001283 if (client_framer_.version().HasHeaderProtection()) {
1284 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1285 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001286 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1287 }
1288 ProcessPacket(serialized);
1289 EXPECT_EQ(GetParam().version_serialization, header.version_flag);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001290 DeleteFrames(&frames_);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001291}
1292
1293TEST_P(QuicPacketCreatorTest, ConsumeDataLargerThanOneStreamFrame) {
1294 if (!GetParam().version_serialization) {
1295 creator_.StopSendingVersion();
1296 }
QUICHE teamdc41bf12019-03-20 12:58:42 -07001297 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001298 // A string larger than fits into a frame.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001299 QuicFrame frame;
ianswett3085da82019-04-04 07:24:24 -07001300 size_t payload_length = creator_.max_packet_length();
1301 const std::string too_long_payload(payload_length, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001302 MakeIOVector(too_long_payload, &iov_);
1303 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1304 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
QUICHE teamdc41bf12019-03-20 12:58:42 -07001305 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1306 client_framer_.transport_version(), Perspective::IS_CLIENT);
1307 ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u,
1308 true, false, NOT_RETRANSMISSION, &frame));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001309 size_t consumed = frame.stream_frame.data_length;
ianswett3085da82019-04-04 07:24:24 -07001310 // The entire payload could not be consumed.
1311 EXPECT_GT(payload_length, consumed);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001312 creator_.Flush();
1313 DeleteSerializedPacket();
1314}
1315
1316TEST_P(QuicPacketCreatorTest, AddFrameAndFlush) {
1317 if (!GetParam().version_serialization) {
1318 creator_.StopSendingVersion();
1319 }
1320 const size_t max_plaintext_size =
1321 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1322 EXPECT_FALSE(creator_.HasPendingFrames());
nharper46833c32019-05-15 21:33:05 -07001323 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
1324 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1325 client_framer_.transport_version(), Perspective::IS_CLIENT);
1326 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1327 stream_id =
1328 QuicUtils::GetCryptoStreamId(client_framer_.transport_version());
1329 }
1330 EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001331 EXPECT_EQ(max_plaintext_size -
1332 GetPacketHeaderSize(
1333 client_framer_.transport_version(),
1334 creator_.GetDestinationConnectionIdLength(),
1335 creator_.GetSourceConnectionIdLength(),
1336 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
1337 !kIncludeDiversificationNonce,
1338 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_),
1339 QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_),
1340 0, QuicPacketCreatorPeer::GetLengthLength(&creator_)),
1341 creator_.BytesFree());
rchc76cd742019-03-26 16:00:03 -07001342 StrictMock<MockDebugDelegate> debug;
1343 creator_.set_debug_delegate(&debug);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001344
1345 // Add a variety of frame types and then a padding frame.
1346 QuicAckFrame ack_frame(InitAckFrame(10u));
rchc76cd742019-03-26 16:00:03 -07001347 EXPECT_CALL(debug, OnFrameAddedToPacket(_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001348 EXPECT_TRUE(
1349 creator_.AddSavedFrame(QuicFrame(&ack_frame), NOT_RETRANSMISSION));
1350 EXPECT_TRUE(creator_.HasPendingFrames());
nharper46833c32019-05-15 21:33:05 -07001351 EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001352
1353 QuicFrame frame;
1354 MakeIOVector("test", &iov_);
rchc76cd742019-03-26 16:00:03 -07001355 EXPECT_CALL(debug, OnFrameAddedToPacket(_));
nharper46833c32019-05-15 21:33:05 -07001356 ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u,
1357 false, false, NOT_RETRANSMISSION, &frame));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001358 size_t consumed = frame.stream_frame.data_length;
1359 EXPECT_EQ(4u, consumed);
1360 EXPECT_TRUE(creator_.HasPendingFrames());
nharper46833c32019-05-15 21:33:05 -07001361 EXPECT_TRUE(creator_.HasPendingStreamFramesOfStream(stream_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001362
1363 QuicPaddingFrame padding_frame;
rchc76cd742019-03-26 16:00:03 -07001364 EXPECT_CALL(debug, OnFrameAddedToPacket(_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001365 EXPECT_TRUE(
1366 creator_.AddSavedFrame(QuicFrame(padding_frame), NOT_RETRANSMISSION));
1367 EXPECT_TRUE(creator_.HasPendingFrames());
1368 EXPECT_EQ(0u, creator_.BytesFree());
1369
1370 // Packet is full. Creator will flush.
1371 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1372 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1373 EXPECT_FALSE(
1374 creator_.AddSavedFrame(QuicFrame(&ack_frame), NOT_RETRANSMISSION));
1375
1376 // Ensure the packet is successfully created.
1377 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
1378 ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty());
1379 const QuicFrames& retransmittable = serialized_packet_.retransmittable_frames;
1380 ASSERT_EQ(1u, retransmittable.size());
1381 EXPECT_EQ(STREAM_FRAME, retransmittable[0].type);
1382 EXPECT_TRUE(serialized_packet_.has_ack);
1383 EXPECT_EQ(QuicPacketNumber(10u), serialized_packet_.largest_acked);
1384 DeleteSerializedPacket();
1385
1386 EXPECT_FALSE(creator_.HasPendingFrames());
nharper46833c32019-05-15 21:33:05 -07001387 EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001388 EXPECT_EQ(max_plaintext_size -
1389 GetPacketHeaderSize(
1390 client_framer_.transport_version(),
1391 creator_.GetDestinationConnectionIdLength(),
1392 creator_.GetSourceConnectionIdLength(),
1393 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
1394 !kIncludeDiversificationNonce,
1395 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_),
1396 QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_),
1397 0, QuicPacketCreatorPeer::GetLengthLength(&creator_)),
1398 creator_.BytesFree());
1399}
1400
1401TEST_P(QuicPacketCreatorTest, SerializeAndSendStreamFrame) {
1402 if (!GetParam().version_serialization) {
1403 creator_.StopSendingVersion();
1404 }
1405 EXPECT_FALSE(creator_.HasPendingFrames());
1406
1407 MakeIOVector("test", &iov_);
dschinazi552accc2019-06-17 17:07:34 -07001408 producer_.SaveStreamData(GetNthClientInitiatedStreamId(0), &iov_, 1u, 0u,
1409 iov_.iov_len);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001410 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1411 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1412 size_t num_bytes_consumed;
rchc76cd742019-03-26 16:00:03 -07001413 StrictMock<MockDebugDelegate> debug;
1414 creator_.set_debug_delegate(&debug);
1415 EXPECT_CALL(debug, OnFrameAddedToPacket(_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001416 creator_.CreateAndSerializeStreamFrame(
dschinazi552accc2019-06-17 17:07:34 -07001417 GetNthClientInitiatedStreamId(0), iov_.iov_len, 0, 0, true,
1418 NOT_RETRANSMISSION, &num_bytes_consumed);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001419 EXPECT_EQ(4u, num_bytes_consumed);
1420
1421 // Ensure the packet is successfully created.
1422 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
1423 ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty());
1424 const QuicFrames& retransmittable = serialized_packet_.retransmittable_frames;
1425 ASSERT_EQ(1u, retransmittable.size());
1426 EXPECT_EQ(STREAM_FRAME, retransmittable[0].type);
1427 DeleteSerializedPacket();
1428
1429 EXPECT_FALSE(creator_.HasPendingFrames());
1430}
1431
nharperebabffd2019-06-03 17:34:45 -07001432TEST_P(QuicPacketCreatorTest, SerializeStreamFrameWithPadding) {
1433 // Regression test to check that CreateAndSerializeStreamFrame uses a
1434 // correctly formatted stream frame header when appending padding.
1435
1436 if (!GetParam().version_serialization) {
1437 creator_.StopSendingVersion();
1438 }
1439 EXPECT_FALSE(creator_.HasPendingFrames());
1440
1441 // Send one byte of stream data.
1442 MakeIOVector("a", &iov_);
dschinazi552accc2019-06-17 17:07:34 -07001443 producer_.SaveStreamData(GetNthClientInitiatedStreamId(0), &iov_, 1u, 0u,
1444 iov_.iov_len);
nharperebabffd2019-06-03 17:34:45 -07001445 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1446 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1447 size_t num_bytes_consumed;
1448 creator_.CreateAndSerializeStreamFrame(
dschinazi552accc2019-06-17 17:07:34 -07001449 GetNthClientInitiatedStreamId(0), iov_.iov_len, 0, 0, true,
1450 NOT_RETRANSMISSION, &num_bytes_consumed);
nharperebabffd2019-06-03 17:34:45 -07001451 EXPECT_EQ(1u, num_bytes_consumed);
1452
1453 // Check that a packet is created.
1454 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
1455 ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty());
1456 {
1457 InSequence s;
1458 EXPECT_CALL(framer_visitor_, OnPacket());
1459 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1460 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1461 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1462 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1463 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1464 if (client_framer_.version().HasHeaderProtection()) {
1465 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1466 }
1467 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1468 }
1469 ProcessPacket(serialized_packet_);
1470}
1471
QUICHE teama6ef0a62019-03-07 20:34:33 -05001472TEST_P(QuicPacketCreatorTest, AddUnencryptedStreamDataClosesConnection) {
1473 // EXPECT_QUIC_BUG tests are expensive so only run one instance of them.
1474 if (!IsDefaultTestConfiguration()) {
1475 return;
1476 }
1477
QUICHE team6987b4a2019-03-15 16:23:04 -07001478 creator_.set_encryption_level(ENCRYPTION_INITIAL);
fkastenholz85f18902019-05-28 12:47:00 -07001479 EXPECT_CALL(delegate_, OnUnrecoverableError(_, _));
dschinazi552accc2019-06-17 17:07:34 -07001480 QuicStreamFrame stream_frame(GetNthClientInitiatedStreamId(0),
1481 /*fin=*/false, 0u, QuicStringPiece());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001482 EXPECT_QUIC_BUG(
1483 creator_.AddSavedFrame(QuicFrame(stream_frame), NOT_RETRANSMISSION),
fayang49523232019-05-03 06:28:22 -07001484 "Cannot send stream data with level: ENCRYPTION_INITIAL");
1485}
1486
1487TEST_P(QuicPacketCreatorTest, SendStreamDataWithEncryptionHandshake) {
1488 // EXPECT_QUIC_BUG tests are expensive so only run one instance of them.
1489 if (!IsDefaultTestConfiguration()) {
1490 return;
1491 }
1492
1493 creator_.set_encryption_level(ENCRYPTION_HANDSHAKE);
fkastenholz85f18902019-05-28 12:47:00 -07001494 EXPECT_CALL(delegate_, OnUnrecoverableError(_, _));
dschinazi552accc2019-06-17 17:07:34 -07001495 QuicStreamFrame stream_frame(GetNthClientInitiatedStreamId(0),
1496 /*fin=*/false, 0u, QuicStringPiece());
fayang49523232019-05-03 06:28:22 -07001497 EXPECT_QUIC_BUG(
1498 creator_.AddSavedFrame(QuicFrame(stream_frame), NOT_RETRANSMISSION),
1499 "Cannot send stream data with level: ENCRYPTION_HANDSHAKE");
QUICHE teama6ef0a62019-03-07 20:34:33 -05001500}
1501
1502TEST_P(QuicPacketCreatorTest, ChloTooLarge) {
1503 // EXPECT_QUIC_BUG tests are expensive so only run one instance of them.
1504 if (!IsDefaultTestConfiguration()) {
1505 return;
1506 }
1507
QUICHE teamdc41bf12019-03-20 12:58:42 -07001508 // This test only matters when the crypto handshake is sent in stream frames.
1509 // TODO(b/128596274): Re-enable when this check is supported for CRYPTO
1510 // frames.
1511 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1512 return;
1513 }
1514
QUICHE teama6ef0a62019-03-07 20:34:33 -05001515 CryptoHandshakeMessage message;
1516 message.set_tag(kCHLO);
dschinazi66dea072019-04-09 11:41:06 -07001517 message.set_minimum_size(kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001518 CryptoFramer framer;
1519 std::unique_ptr<QuicData> message_data;
QUICHE team3fe6a8b2019-03-14 09:10:38 -07001520 message_data = framer.ConstructHandshakeMessage(message);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001521
1522 struct iovec iov;
1523 MakeIOVector(QuicStringPiece(message_data->data(), message_data->length()),
1524 &iov);
1525 QuicFrame frame;
fkastenholz85f18902019-05-28 12:47:00 -07001526 EXPECT_CALL(delegate_, OnUnrecoverableError(QUIC_CRYPTO_CHLO_TOO_LARGE, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001527 EXPECT_QUIC_BUG(creator_.ConsumeData(QuicUtils::GetCryptoStreamId(
1528 client_framer_.transport_version()),
1529 &iov, 1u, iov.iov_len, 0u, 0u, false,
1530 false, NOT_RETRANSMISSION, &frame),
1531 "Client hello won't fit in a single packet.");
1532}
1533
1534TEST_P(QuicPacketCreatorTest, PendingPadding) {
1535 EXPECT_EQ(0u, creator_.pending_padding_bytes());
1536 creator_.AddPendingPadding(kMaxNumRandomPaddingBytes * 10);
1537 EXPECT_EQ(kMaxNumRandomPaddingBytes * 10, creator_.pending_padding_bytes());
1538
1539 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1540 .WillRepeatedly(
1541 Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1542 // Flush all paddings.
1543 while (creator_.pending_padding_bytes() > 0) {
1544 creator_.Flush();
1545 {
1546 InSequence s;
1547 EXPECT_CALL(framer_visitor_, OnPacket());
1548 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1549 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1550 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1551 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1552 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1553 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1554 }
1555 // Packet only contains padding.
1556 ProcessPacket(serialized_packet_);
1557 }
1558 EXPECT_EQ(0u, creator_.pending_padding_bytes());
1559}
1560
1561TEST_P(QuicPacketCreatorTest, FullPaddingDoesNotConsumePendingPadding) {
QUICHE teamdc41bf12019-03-20 12:58:42 -07001562 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001563 creator_.AddPendingPadding(kMaxNumRandomPaddingBytes);
1564 QuicFrame frame;
1565 MakeIOVector("test", &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001566 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1567 client_framer_.transport_version(), Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001568 ASSERT_TRUE(creator_.ConsumeData(
QUICHE teamdc41bf12019-03-20 12:58:42 -07001569 stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001570 /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame));
1571 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1572 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1573 creator_.Flush();
1574 EXPECT_EQ(kMaxNumRandomPaddingBytes, creator_.pending_padding_bytes());
1575}
1576
1577TEST_P(QuicPacketCreatorTest, SendPendingPaddingInRetransmission) {
QUICHE teamdc41bf12019-03-20 12:58:42 -07001578 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
1579 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1580 client_framer_.transport_version(), Perspective::IS_CLIENT);
1581 QuicStreamFrame stream_frame(stream_id,
1582 /*fin=*/false, 0u, QuicStringPiece());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001583 QuicFrames frames;
1584 frames.push_back(QuicFrame(stream_frame));
dschinazi66dea072019-04-09 11:41:06 -07001585 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05001586 QuicPendingRetransmission retransmission(CreateRetransmission(
QUICHE teamdc41bf12019-03-20 12:58:42 -07001587 frames, true, /*num_padding_bytes=*/0, ENCRYPTION_FORWARD_SECURE,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001588 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
1589 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1590 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1591 creator_.AddPendingPadding(kMaxNumRandomPaddingBytes);
dschinazi66dea072019-04-09 11:41:06 -07001592 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001593 EXPECT_EQ(0u, creator_.pending_padding_bytes());
1594 {
1595 InSequence s;
1596 EXPECT_CALL(framer_visitor_, OnPacket());
1597 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1598 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1599 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1600 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1601 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1602 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1603 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1604 }
1605 ProcessPacket(serialized_packet_);
1606}
1607
1608TEST_P(QuicPacketCreatorTest, SendPacketAfterFullPaddingRetransmission) {
1609 // Making sure needs_full_padding gets reset after a full padding
1610 // retransmission.
1611 EXPECT_EQ(0u, creator_.pending_padding_bytes());
QUICHE teamdc41bf12019-03-20 12:58:42 -07001612 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001613 QuicFrame frame;
QUICHE teamdc41bf12019-03-20 12:58:42 -07001614 std::string data = "fake handshake message data";
1615 MakeIOVector(data, &iov_);
1616 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1617 client_framer_.transport_version(), Perspective::IS_CLIENT);
1618 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1619 stream_id =
1620 QuicUtils::GetCryptoStreamId(client_framer_.transport_version());
1621 }
1622 producer_.SaveStreamData(stream_id, &iov_, 1u, 0u, iov_.iov_len);
1623 QuicPacketCreatorPeer::CreateStreamFrame(&creator_, stream_id, iov_.iov_len,
1624 0u, false, &frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001625 QuicFrames frames;
1626 frames.push_back(frame);
dschinazi66dea072019-04-09 11:41:06 -07001627 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05001628 QuicPendingRetransmission retransmission(CreateRetransmission(
QUICHE teamdc41bf12019-03-20 12:58:42 -07001629 frames, true, /*num_padding_bytes=*/-1, ENCRYPTION_FORWARD_SECURE,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001630 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)));
1631 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1632 .WillRepeatedly(
1633 Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
dschinazi66dea072019-04-09 11:41:06 -07001634 creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001635 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length);
1636 {
1637 InSequence s;
1638 EXPECT_CALL(framer_visitor_, OnPacket());
1639 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1640 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1641 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1642 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1643 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1644 // Full padding.
1645 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1646 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1647 }
1648 ProcessPacket(serialized_packet_);
1649
QUICHE teamdc41bf12019-03-20 12:58:42 -07001650 creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, false,
1651 NOT_RETRANSMISSION, &frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001652 creator_.Flush();
1653 {
1654 InSequence s;
1655 EXPECT_CALL(framer_visitor_, OnPacket());
1656 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1657 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1658 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1659 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
1660 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1661 // needs_full_padding gets reset.
1662 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)).Times(0);
1663 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1664 }
1665 ProcessPacket(serialized_packet_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001666 DeleteFrames(&frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001667}
1668
1669TEST_P(QuicPacketCreatorTest, ConsumeDataAndRandomPadding) {
QUICHE teamdc41bf12019-03-20 12:58:42 -07001670 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001671 const QuicByteCount kStreamFramePayloadSize = 100u;
1672 // Set the packet size be enough for one stream frame with 0 stream offset +
1673 // 1.
QUICHE teamdc41bf12019-03-20 12:58:42 -07001674 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1675 client_framer_.transport_version(), Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001676 size_t length =
1677 GetPacketHeaderOverhead(client_framer_.transport_version()) +
1678 GetEncryptionOverhead() +
1679 QuicFramer::GetMinStreamFrameSize(
QUICHE teamdc41bf12019-03-20 12:58:42 -07001680 client_framer_.transport_version(), stream_id, 0,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001681 /*last_frame_in_packet=*/false, kStreamFramePayloadSize + 1) +
1682 kStreamFramePayloadSize + 1;
1683 creator_.SetMaxPacketLength(length);
1684 creator_.AddPendingPadding(kMaxNumRandomPaddingBytes);
1685 QuicByteCount pending_padding_bytes = creator_.pending_padding_bytes();
1686 QuicFrame frame;
1687 char buf[kStreamFramePayloadSize + 1] = {};
1688 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1689 .WillRepeatedly(
1690 Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1691 // Send stream frame of size kStreamFramePayloadSize.
1692 MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001693 creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, false,
1694 NOT_RETRANSMISSION, &frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001695 creator_.Flush();
1696 // 1 byte padding is sent.
1697 EXPECT_EQ(pending_padding_bytes - 1, creator_.pending_padding_bytes());
1698 // Send stream frame of size kStreamFramePayloadSize + 1.
1699 MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize + 1), &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001700 creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u,
1701 kStreamFramePayloadSize, false, false,
1702 NOT_RETRANSMISSION, &frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001703 // No padding is sent.
1704 creator_.Flush();
1705 EXPECT_EQ(pending_padding_bytes - 1, creator_.pending_padding_bytes());
1706 // Flush all paddings.
1707 while (creator_.pending_padding_bytes() > 0) {
1708 creator_.Flush();
1709 }
1710 EXPECT_EQ(0u, creator_.pending_padding_bytes());
1711}
1712
1713TEST_P(QuicPacketCreatorTest, FlushWithExternalBuffer) {
QUICHE teamdc41bf12019-03-20 12:58:42 -07001714 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
dschinazi66dea072019-04-09 11:41:06 -07001715 char external_buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05001716 char* expected_buffer = external_buffer;
1717 EXPECT_CALL(delegate_, GetPacketBuffer()).WillOnce(Return(expected_buffer));
1718
1719 QuicFrame frame;
1720 MakeIOVector("test", &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001721 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1722 client_framer_.transport_version(), Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001723 ASSERT_TRUE(creator_.ConsumeData(
QUICHE teamdc41bf12019-03-20 12:58:42 -07001724 stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001725 /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame));
1726
1727 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1728 .WillOnce(Invoke([expected_buffer](SerializedPacket* serialized_packet) {
1729 EXPECT_EQ(expected_buffer, serialized_packet->encrypted_buffer);
1730 ClearSerializedPacket(serialized_packet);
1731 }));
1732 creator_.Flush();
1733}
1734
1735// Test for error found in
1736// https://bugs.chromium.org/p/chromium/issues/detail?id=859949 where a gap
1737// length that crosses an IETF VarInt length boundary would cause a
1738// failure. While this test is not applicable to versions other than version 99,
1739// it should still work. Hence, it is not made version-specific.
1740TEST_P(QuicPacketCreatorTest, IetfAckGapErrorRegression) {
1741 QuicAckFrame ack_frame =
1742 InitAckFrame({{QuicPacketNumber(60), QuicPacketNumber(61)},
1743 {QuicPacketNumber(125), QuicPacketNumber(126)}});
1744 frames_.push_back(QuicFrame(&ack_frame));
1745 SerializeAllFrames(frames_);
1746}
1747
1748TEST_P(QuicPacketCreatorTest, AddMessageFrame) {
fayangd4291e42019-05-30 10:31:21 -07001749 if (!VersionSupportsMessageFrames(client_framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001750 return;
1751 }
QUICHE teamdc41bf12019-03-20 12:58:42 -07001752 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001753 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1754 .Times(3)
1755 .WillRepeatedly(
1756 Invoke(this, &QuicPacketCreatorTest::ClearSerializedPacketForTests));
1757 QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
1758 // Verify that there is enough room for the largest message payload.
ianswettb239f862019-04-05 09:15:06 -07001759 EXPECT_TRUE(creator_.HasRoomForMessageFrame(
1760 creator_.GetCurrentLargestMessagePayload()));
1761 std::string message(creator_.GetCurrentLargestMessagePayload(), 'a');
wub553a9662019-03-28 20:13:23 -07001762 QuicMessageFrame* message_frame =
1763 new QuicMessageFrame(1, MakeSpan(&allocator_, message, &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001764 EXPECT_TRUE(
1765 creator_.AddSavedFrame(QuicFrame(message_frame), NOT_RETRANSMISSION));
1766 EXPECT_TRUE(creator_.HasPendingFrames());
1767 creator_.Flush();
1768
wub553a9662019-03-28 20:13:23 -07001769 QuicMessageFrame* frame2 =
1770 new QuicMessageFrame(2, MakeSpan(&allocator_, "message", &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001771 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame2), NOT_RETRANSMISSION));
1772 EXPECT_TRUE(creator_.HasPendingFrames());
1773 // Verify if a new frame is added, 1 byte message length will be added.
1774 EXPECT_EQ(1u, creator_.ExpansionOnNewFrame());
wub553a9662019-03-28 20:13:23 -07001775 QuicMessageFrame* frame3 =
1776 new QuicMessageFrame(3, MakeSpan(&allocator_, "message2", &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001777 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame3), NOT_RETRANSMISSION));
1778 EXPECT_EQ(1u, creator_.ExpansionOnNewFrame());
1779 creator_.Flush();
1780
1781 QuicFrame frame;
1782 MakeIOVector("test", &iov_);
QUICHE teamdc41bf12019-03-20 12:58:42 -07001783 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1784 client_framer_.transport_version(), Perspective::IS_CLIENT);
1785 EXPECT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u,
1786 false, false, NOT_RETRANSMISSION, &frame));
wub553a9662019-03-28 20:13:23 -07001787 QuicMessageFrame* frame4 =
1788 new QuicMessageFrame(4, MakeSpan(&allocator_, "message", &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001789 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame4), NOT_RETRANSMISSION));
1790 EXPECT_TRUE(creator_.HasPendingFrames());
1791 // Verify there is not enough room for largest payload.
ianswettb239f862019-04-05 09:15:06 -07001792 EXPECT_FALSE(creator_.HasRoomForMessageFrame(
1793 creator_.GetCurrentLargestMessagePayload()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001794 // Add largest message will causes the flush of the stream frame.
wub553a9662019-03-28 20:13:23 -07001795 QuicMessageFrame frame5(5, MakeSpan(&allocator_, message, &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001796 EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&frame5), NOT_RETRANSMISSION));
1797 EXPECT_FALSE(creator_.HasPendingFrames());
1798}
1799
1800TEST_P(QuicPacketCreatorTest, MessageFrameConsumption) {
fayangd4291e42019-05-30 10:31:21 -07001801 if (!VersionSupportsMessageFrames(client_framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001802 return;
1803 }
vasilvvc48c8712019-03-11 13:38:16 -07001804 std::string message_data(kDefaultMaxPacketSize, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001805 QuicStringPiece message_buffer(message_data);
1806 QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
ianswettb239f862019-04-05 09:15:06 -07001807 // Test all possible encryption levels of message frames.
1808 for (EncryptionLevel level :
1809 {ENCRYPTION_ZERO_RTT, ENCRYPTION_FORWARD_SECURE}) {
1810 creator_.set_encryption_level(level);
1811 // Test all possible sizes of message frames.
1812 for (size_t message_size = 0;
1813 message_size <= creator_.GetCurrentLargestMessagePayload();
1814 ++message_size) {
1815 QuicMessageFrame* frame = new QuicMessageFrame(
1816 0, MakeSpan(&allocator_,
1817 QuicStringPiece(message_buffer.data(), message_size),
1818 &storage));
1819 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame), NOT_RETRANSMISSION));
1820 EXPECT_TRUE(creator_.HasPendingFrames());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001821
ianswettb239f862019-04-05 09:15:06 -07001822 size_t expansion_bytes = message_size >= 64 ? 2 : 1;
1823 EXPECT_EQ(expansion_bytes, creator_.ExpansionOnNewFrame());
1824 // Verify BytesFree returns bytes available for the next frame, which
1825 // should subtract the message length.
1826 size_t expected_bytes_free =
1827 creator_.GetCurrentLargestMessagePayload() - message_size <
1828 expansion_bytes
1829 ? 0
1830 : creator_.GetCurrentLargestMessagePayload() - expansion_bytes -
1831 message_size;
1832 EXPECT_EQ(expected_bytes_free, creator_.BytesFree());
1833 EXPECT_LE(creator_.GetGuaranteedLargestMessagePayload(),
1834 creator_.GetCurrentLargestMessagePayload());
1835 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1836 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1837 creator_.Flush();
1838 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
1839 DeleteSerializedPacket();
1840 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001841 }
1842}
1843
nharperd43f1d62019-07-01 15:18:20 -07001844// Regression test for bugfix of GetPacketHeaderSize.
1845TEST_P(QuicPacketCreatorTest, GetGuaranteedLargestMessagePayload) {
1846 QuicTransportVersion version = GetParam().version.transport_version;
1847 if (!VersionSupportsMessageFrames(version)) {
1848 return;
1849 }
1850 QuicPacketLength expected_largest_payload = 1319;
1851 if (QuicVersionHasLongHeaderLengths(version)) {
1852 expected_largest_payload -= 2;
1853 }
dschinazi48ac9192019-07-31 00:07:26 -07001854 if (GetParam().version.HasLengthPrefixedConnectionIds()) {
1855 expected_largest_payload -= 1;
1856 }
nharperd43f1d62019-07-01 15:18:20 -07001857 EXPECT_EQ(expected_largest_payload,
1858 creator_.GetGuaranteedLargestMessagePayload());
1859}
1860
QUICHE teama6ef0a62019-03-07 20:34:33 -05001861TEST_P(QuicPacketCreatorTest, PacketTransmissionType) {
QUICHE teamdc41bf12019-03-20 12:58:42 -07001862 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001863 creator_.set_can_set_transmission_type(true);
1864 creator_.SetTransmissionType(NOT_RETRANSMISSION);
1865
1866 QuicAckFrame temp_ack_frame = InitAckFrame(1);
1867 QuicFrame ack_frame(&temp_ack_frame);
1868 ASSERT_FALSE(QuicUtils::IsRetransmittableFrame(ack_frame.type));
1869
QUICHE teamdc41bf12019-03-20 12:58:42 -07001870 QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId(
1871 client_framer_.transport_version(), Perspective::IS_CLIENT);
1872 QuicFrame stream_frame(QuicStreamFrame(stream_id,
1873 /*fin=*/false, 0u, QuicStringPiece()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001874 ASSERT_TRUE(QuicUtils::IsRetransmittableFrame(stream_frame.type));
1875
1876 QuicFrame padding_frame{QuicPaddingFrame()};
1877 ASSERT_FALSE(QuicUtils::IsRetransmittableFrame(padding_frame.type));
1878
1879 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1880 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket));
1881
1882 EXPECT_TRUE(creator_.AddSavedFrame(ack_frame, LOSS_RETRANSMISSION));
1883 ASSERT_FALSE(serialized_packet_.encrypted_buffer);
1884
1885 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame, RTO_RETRANSMISSION));
1886 ASSERT_FALSE(serialized_packet_.encrypted_buffer);
1887
1888 EXPECT_TRUE(creator_.AddSavedFrame(padding_frame, TLP_RETRANSMISSION));
1889 creator_.Flush();
1890 ASSERT_TRUE(serialized_packet_.encrypted_buffer);
1891
wub98669f52019-04-18 10:49:18 -07001892 if (creator_.can_set_transmission_type()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001893 // The last retransmittable frame on packet is a stream frame, the packet's
1894 // transmission type should be the same as the stream frame's.
1895 EXPECT_EQ(serialized_packet_.transmission_type, RTO_RETRANSMISSION);
1896 } else {
1897 EXPECT_EQ(serialized_packet_.transmission_type, NOT_RETRANSMISSION);
1898 }
1899 DeleteSerializedPacket();
1900}
1901
1902TEST_P(QuicPacketCreatorTest, RetryToken) {
1903 if (!GetParam().version_serialization ||
1904 !QuicVersionHasLongHeaderLengths(client_framer_.transport_version())) {
1905 return;
1906 }
1907
1908 char retry_token_bytes[] = {1, 2, 3, 4, 5, 6, 7, 8,
1909 9, 10, 11, 12, 13, 14, 15, 16};
1910
1911 creator_.SetRetryToken(
vasilvvc48c8712019-03-11 13:38:16 -07001912 std::string(retry_token_bytes, sizeof(retry_token_bytes)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001913
QUICHE teamdc41bf12019-03-20 12:58:42 -07001914 std::string data("a");
1915 if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1916 QuicStreamFrame stream_frame(
1917 QuicUtils::GetCryptoStreamId(client_framer_.transport_version()),
1918 /*fin=*/false, 0u, QuicStringPiece());
1919 frames_.push_back(QuicFrame(stream_frame));
1920 } else {
1921 producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data);
1922 frames_.push_back(
1923 QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length())));
1924 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001925 SerializedPacket serialized = SerializeAllFrames(frames_);
1926
1927 QuicPacketHeader header;
1928 {
1929 InSequence s;
1930 EXPECT_CALL(framer_visitor_, OnPacket());
1931 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
1932 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
1933 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
1934 EXPECT_CALL(framer_visitor_, OnPacketHeader(_))
1935 .WillOnce(DoAll(SaveArg<0>(&header), Return(true)));
QUICHE teamdc41bf12019-03-20 12:58:42 -07001936 if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) {
1937 EXPECT_CALL(framer_visitor_, OnCryptoFrame(_));
1938 } else {
1939 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
1940 }
nharper55fa6132019-05-07 19:37:21 -07001941 if (client_framer_.version().HasHeaderProtection()) {
1942 EXPECT_CALL(framer_visitor_, OnPaddingFrame(_));
1943 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001944 EXPECT_CALL(framer_visitor_, OnPacketComplete());
1945 }
1946 ProcessPacket(serialized);
1947 ASSERT_TRUE(header.version_flag);
1948 ASSERT_EQ(header.long_packet_type, INITIAL);
1949 ASSERT_EQ(header.retry_token.length(), sizeof(retry_token_bytes));
1950 test::CompareCharArraysWithHexError(
1951 "retry token", header.retry_token.data(), header.retry_token.length(),
1952 retry_token_bytes, sizeof(retry_token_bytes));
QUICHE teamdc41bf12019-03-20 12:58:42 -07001953 DeleteFrames(&frames_);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001954}
1955
QUICHE team2252b702019-05-14 23:55:14 -04001956TEST_P(QuicPacketCreatorTest, GetConnectionId) {
QUICHE team2252b702019-05-14 23:55:14 -04001957 EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId());
1958 EXPECT_EQ(EmptyQuicConnectionId(), creator_.GetSourceConnectionId());
1959}
1960
dschinazi346b7ce2019-06-05 01:38:18 -07001961TEST_P(QuicPacketCreatorTest, ClientConnectionId) {
dschinazi346b7ce2019-06-05 01:38:18 -07001962 if (!client_framer_.version().SupportsClientConnectionIds()) {
1963 return;
1964 }
1965 EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId());
1966 EXPECT_EQ(EmptyQuicConnectionId(), creator_.GetSourceConnectionId());
1967 creator_.SetClientConnectionId(TestConnectionId(0x33));
1968 EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId());
1969 EXPECT_EQ(TestConnectionId(0x33), creator_.GetSourceConnectionId());
1970}
1971
QUICHE teama6ef0a62019-03-07 20:34:33 -05001972} // namespace
1973} // namespace test
1974} // namespace quic