Improve parsing manifest for FSP API.
[chromium-blink-merge.git] / net / quic / quic_framer_test.cc
blob16383620b5f774653d52294de2414672891b925b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_framer.h"
7 #include <algorithm>
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/containers/hash_tables.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/port.h"
16 #include "base/stl_util.h"
17 #include "net/quic/crypto/quic_decrypter.h"
18 #include "net/quic/crypto/quic_encrypter.h"
19 #include "net/quic/quic_protocol.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/test_tools/quic_framer_peer.h"
22 #include "net/quic/test_tools/quic_test_utils.h"
23 #include "net/test/gtest_util.h"
25 using base::hash_set;
26 using base::StringPiece;
27 using std::make_pair;
28 using std::map;
29 using std::numeric_limits;
30 using std::pair;
31 using std::string;
32 using std::vector;
33 using testing::Return;
34 using testing::_;
36 namespace net {
37 namespace test {
39 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
40 const QuicPacketSequenceNumber kMask = kEpoch - 1;
42 // Index into the connection_id offset in the header.
43 const size_t kConnectionIdOffset = kPublicFlagsSize;
44 // Index into the version string in the header. (if present).
45 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
47 // Size in bytes of the stream frame fields for an arbitrary StreamID and
48 // offset and the last frame in a packet.
49 size_t GetMinStreamFrameSize() {
50 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
53 // Index into the sequence number offset in the header.
54 size_t GetSequenceNumberOffset(QuicConnectionIdLength connection_id_length,
55 bool include_version) {
56 return kConnectionIdOffset + connection_id_length +
57 (include_version ? kQuicVersionSize : 0);
60 size_t GetSequenceNumberOffset(bool include_version) {
61 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
64 // Index into the private flags offset in the data packet header.
65 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length,
66 bool include_version) {
67 return GetSequenceNumberOffset(connection_id_length, include_version) +
68 PACKET_6BYTE_SEQUENCE_NUMBER;
71 size_t GetPrivateFlagsOffset(bool include_version) {
72 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
75 size_t GetPrivateFlagsOffset(bool include_version,
76 QuicSequenceNumberLength sequence_number_length) {
77 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
78 sequence_number_length;
81 // Index into the fec group offset in the header.
82 size_t GetFecGroupOffset(QuicConnectionIdLength connection_id_length,
83 bool include_version) {
84 return GetPrivateFlagsOffset(connection_id_length, include_version) +
85 kPrivateFlagsSize;
88 size_t GetFecGroupOffset(bool include_version) {
89 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
90 kPrivateFlagsSize;
93 size_t GetFecGroupOffset(bool include_version,
94 QuicSequenceNumberLength sequence_number_length) {
95 return GetPrivateFlagsOffset(include_version, sequence_number_length) +
96 kPrivateFlagsSize;
99 // Index into the message tag of the public reset packet.
100 // Public resets always have full connection_ids.
101 const size_t kPublicResetPacketMessageTagOffset =
102 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
104 class TestEncrypter : public QuicEncrypter {
105 public:
106 ~TestEncrypter() override {}
107 bool SetKey(StringPiece key) override { return true; }
108 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
109 bool Encrypt(StringPiece nonce,
110 StringPiece associated_data,
111 StringPiece plaintext,
112 unsigned char* output) override {
113 CHECK(false) << "Not implemented";
114 return false;
116 bool EncryptPacket(QuicPacketSequenceNumber sequence_number,
117 StringPiece associated_data,
118 StringPiece plaintext,
119 char* output,
120 size_t* output_length,
121 size_t max_output_length) override {
122 sequence_number_ = sequence_number;
123 associated_data_ = associated_data.as_string();
124 plaintext_ = plaintext.as_string();
125 memcpy(output, plaintext.data(), plaintext.length());
126 *output_length = plaintext.length();
127 return true;
129 size_t GetKeySize() const override { return 0; }
130 size_t GetNoncePrefixSize() const override { return 0; }
131 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override {
132 return ciphertext_size;
134 size_t GetCiphertextSize(size_t plaintext_size) const override {
135 return plaintext_size;
137 StringPiece GetKey() const override { return StringPiece(); }
138 StringPiece GetNoncePrefix() const override { return StringPiece(); }
139 QuicPacketSequenceNumber sequence_number_;
140 string associated_data_;
141 string plaintext_;
144 class TestDecrypter : public QuicDecrypter {
145 public:
146 ~TestDecrypter() override {}
147 bool SetKey(StringPiece key) override { return true; }
148 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
149 bool DecryptPacket(QuicPacketSequenceNumber sequence_number,
150 const StringPiece& associated_data,
151 const StringPiece& ciphertext,
152 char* output,
153 size_t* output_length,
154 size_t max_output_length) override {
155 sequence_number_ = sequence_number;
156 associated_data_ = associated_data.as_string();
157 ciphertext_ = ciphertext.as_string();
158 memcpy(output, ciphertext.data(), ciphertext.length());
159 *output_length = ciphertext.length();
160 return true;
162 StringPiece GetKey() const override { return StringPiece(); }
163 StringPiece GetNoncePrefix() const override { return StringPiece(); }
164 QuicPacketSequenceNumber sequence_number_;
165 string associated_data_;
166 string ciphertext_;
169 class TestQuicVisitor : public QuicFramerVisitorInterface {
170 public:
171 TestQuicVisitor()
172 : error_count_(0),
173 version_mismatch_(0),
174 packet_count_(0),
175 frame_count_(0),
176 fec_count_(0),
177 complete_packets_(0),
178 revived_packets_(0),
179 accept_packet_(true),
180 accept_public_header_(true) {
183 ~TestQuicVisitor() override {
184 STLDeleteElements(&stream_frames_);
185 STLDeleteElements(&ack_frames_);
186 STLDeleteElements(&stop_waiting_frames_);
187 STLDeleteElements(&ping_frames_);
188 STLDeleteElements(&fec_data_);
189 STLDeleteElements(&stream_data_);
190 STLDeleteElements(&fec_data_redundancy_);
193 void OnError(QuicFramer* f) override {
194 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
195 << " (" << f->error() << ")";
196 ++error_count_;
199 void OnPacket() override {}
201 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {
202 public_reset_packet_.reset(new QuicPublicResetPacket(packet));
205 void OnVersionNegotiationPacket(
206 const QuicVersionNegotiationPacket& packet) override {
207 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
210 void OnRevivedPacket() override { ++revived_packets_; }
212 bool OnProtocolVersionMismatch(QuicVersion version) override {
213 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version;
214 ++version_mismatch_;
215 return true;
218 bool OnUnauthenticatedPublicHeader(
219 const QuicPacketPublicHeader& header) override {
220 public_header_.reset(new QuicPacketPublicHeader(header));
221 return accept_public_header_;
224 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override {
225 return true;
228 void OnDecryptedPacket(EncryptionLevel level) override {}
230 bool OnPacketHeader(const QuicPacketHeader& header) override {
231 ++packet_count_;
232 header_.reset(new QuicPacketHeader(header));
233 return accept_packet_;
236 bool OnStreamFrame(const QuicStreamFrame& frame) override {
237 ++frame_count_;
238 // Save a copy of the data so it is valid after the packet is processed.
239 stream_data_.push_back(frame.GetDataAsString());
240 QuicStreamFrame* stream_frame = new QuicStreamFrame(frame);
241 // Make sure that the stream frame points to this data.
242 stream_frame->data.Clear();
243 stream_frame->data.Append(const_cast<char*>(stream_data_.back()->data()),
244 stream_data_.back()->size());
245 stream_frames_.push_back(stream_frame);
246 return true;
249 void OnFecProtectedPayload(StringPiece payload) override {
250 fec_protected_payload_ = payload.as_string();
253 bool OnAckFrame(const QuicAckFrame& frame) override {
254 ++frame_count_;
255 ack_frames_.push_back(new QuicAckFrame(frame));
256 return true;
259 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override {
260 ++frame_count_;
261 stop_waiting_frames_.push_back(new QuicStopWaitingFrame(frame));
262 return true;
265 bool OnPingFrame(const QuicPingFrame& frame) override {
266 ++frame_count_;
267 ping_frames_.push_back(new QuicPingFrame(frame));
268 return true;
271 void OnFecData(const QuicFecData& fec) override {
272 ++fec_count_;
273 QuicFecData* fec_data = new QuicFecData();
274 fec_data->fec_group = fec.fec_group;
275 // Save a copy of the data so it is valid after the packet is processed.
276 string* redundancy = new string(fec.redundancy.as_string());
277 fec_data_redundancy_.push_back(redundancy);
278 fec_data->redundancy = StringPiece(*redundancy);
279 fec_data_.push_back(fec_data);
282 void OnPacketComplete() override { ++complete_packets_; }
284 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override {
285 rst_stream_frame_ = frame;
286 return true;
289 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override {
290 connection_close_frame_ = frame;
291 return true;
294 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override {
295 goaway_frame_ = frame;
296 return true;
299 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
300 window_update_frame_ = frame;
301 return true;
304 bool OnBlockedFrame(const QuicBlockedFrame& frame) override {
305 blocked_frame_ = frame;
306 return true;
309 // Counters from the visitor_ callbacks.
310 int error_count_;
311 int version_mismatch_;
312 int packet_count_;
313 int frame_count_;
314 int fec_count_;
315 int complete_packets_;
316 int revived_packets_;
317 bool accept_packet_;
318 bool accept_public_header_;
320 scoped_ptr<QuicPacketHeader> header_;
321 scoped_ptr<QuicPacketPublicHeader> public_header_;
322 scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
323 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
324 vector<QuicStreamFrame*> stream_frames_;
325 vector<QuicAckFrame*> ack_frames_;
326 vector<QuicStopWaitingFrame*> stop_waiting_frames_;
327 vector<QuicPingFrame*> ping_frames_;
328 vector<QuicFecData*> fec_data_;
329 string fec_protected_payload_;
330 QuicRstStreamFrame rst_stream_frame_;
331 QuicConnectionCloseFrame connection_close_frame_;
332 QuicGoAwayFrame goaway_frame_;
333 QuicWindowUpdateFrame window_update_frame_;
334 QuicBlockedFrame blocked_frame_;
335 vector<string*> stream_data_;
336 vector<string*> fec_data_redundancy_;
339 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
340 public:
341 QuicFramerTest()
342 : encrypter_(new test::TestEncrypter()),
343 decrypter_(new test::TestDecrypter()),
344 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
345 framer_(QuicSupportedVersions(), start_, Perspective::IS_SERVER) {
346 version_ = GetParam();
347 framer_.set_version(version_);
348 framer_.SetDecrypter(decrypter_, ENCRYPTION_NONE);
349 framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
350 framer_.set_visitor(&visitor_);
351 framer_.set_received_entropy_calculator(&entropy_calculator_);
354 // Helper function to get unsigned char representation of digit in the
355 // units place of the current QUIC version number.
356 unsigned char GetQuicVersionDigitOnes() {
357 return static_cast<unsigned char> ('0' + version_%10);
360 // Helper function to get unsigned char representation of digit in the
361 // tens place of the current QUIC version number.
362 unsigned char GetQuicVersionDigitTens() {
363 return static_cast<unsigned char> ('0' + (version_/10)%10);
366 bool CheckEncryption(QuicPacketSequenceNumber sequence_number,
367 QuicPacket* packet) {
368 if (sequence_number != encrypter_->sequence_number_) {
369 LOG(ERROR) << "Encrypted incorrect packet sequence number. expected "
370 << sequence_number << " actual: "
371 << encrypter_->sequence_number_;
372 return false;
374 if (packet->AssociatedData() != encrypter_->associated_data_) {
375 LOG(ERROR) << "Encrypted incorrect associated data. expected "
376 << packet->AssociatedData() << " actual: "
377 << encrypter_->associated_data_;
378 return false;
380 if (packet->Plaintext() != encrypter_->plaintext_) {
381 LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
382 << packet->Plaintext() << " actual: "
383 << encrypter_->plaintext_;
384 return false;
386 return true;
389 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
390 bool includes_version) {
391 if (visitor_.header_->packet_sequence_number !=
392 decrypter_->sequence_number_) {
393 LOG(ERROR) << "Decrypted incorrect packet sequence number. expected "
394 << visitor_.header_->packet_sequence_number << " actual: "
395 << decrypter_->sequence_number_;
396 return false;
398 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
399 encrypted, PACKET_8BYTE_CONNECTION_ID,
400 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER) !=
401 decrypter_->associated_data_) {
402 LOG(ERROR) << "Decrypted incorrect associated data. expected "
403 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
404 encrypted, PACKET_8BYTE_CONNECTION_ID,
405 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER)
406 << " actual: " << decrypter_->associated_data_;
407 return false;
409 StringPiece ciphertext(encrypted.AsStringPiece().substr(
410 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID, includes_version,
411 PACKET_6BYTE_SEQUENCE_NUMBER)));
412 if (ciphertext != decrypter_->ciphertext_) {
413 LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
414 << ciphertext << " actual: "
415 << decrypter_->ciphertext_;
416 return false;
418 return true;
421 char* AsChars(unsigned char* data) {
422 return reinterpret_cast<char*>(data);
425 void CheckProcessingFails(unsigned char* packet,
426 size_t len,
427 string expected_error,
428 QuicErrorCode error_code) {
429 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
430 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
431 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
432 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
435 // Checks if the supplied string matches data in the supplied StreamFrame.
436 void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
437 scoped_ptr<string> frame_data(frame->GetDataAsString());
438 EXPECT_EQ(str, *frame_data);
441 void CheckStreamFrameBoundaries(unsigned char* packet,
442 size_t stream_id_size,
443 bool include_version) {
444 // Now test framing boundaries.
445 for (size_t i = kQuicFrameTypeSize; i < GetMinStreamFrameSize(); ++i) {
446 string expected_error;
447 if (i < kQuicFrameTypeSize + stream_id_size) {
448 expected_error = "Unable to read stream_id.";
449 } else if (i < kQuicFrameTypeSize + stream_id_size +
450 kQuicMaxStreamOffsetSize) {
451 expected_error = "Unable to read offset.";
452 } else {
453 expected_error = "Unable to read frame data.";
455 CheckProcessingFails(
456 packet,
457 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
458 PACKET_6BYTE_SEQUENCE_NUMBER,
459 NOT_IN_FEC_GROUP),
460 expected_error, QUIC_INVALID_STREAM_DATA);
464 void CheckCalculatePacketSequenceNumber(
465 QuicPacketSequenceNumber expected_sequence_number,
466 QuicPacketSequenceNumber last_sequence_number) {
467 QuicPacketSequenceNumber wire_sequence_number =
468 expected_sequence_number & kMask;
469 QuicFramerPeer::SetLastSequenceNumber(&framer_, last_sequence_number);
470 EXPECT_EQ(expected_sequence_number,
471 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
472 &framer_, PACKET_6BYTE_SEQUENCE_NUMBER, wire_sequence_number))
473 << "last_sequence_number: " << last_sequence_number
474 << " wire_sequence_number: " << wire_sequence_number;
477 QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
478 const QuicFrames& frames) {
479 return BuildUnsizedDataPacket(&framer_, header, frames);
482 QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
483 const QuicFrames& frames,
484 size_t packet_size) {
485 return BuildUnsizedDataPacket(&framer_, header, frames, packet_size);
488 test::TestEncrypter* encrypter_;
489 test::TestDecrypter* decrypter_;
490 QuicVersion version_;
491 QuicTime start_;
492 QuicFramer framer_;
493 test::TestQuicVisitor visitor_;
494 test::TestEntropyCalculator entropy_calculator_;
497 // Run all framer tests with all supported versions of QUIC.
498 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
499 QuicFramerTest,
500 ::testing::ValuesIn(kSupportedQuicVersions));
502 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochStart) {
503 // A few quick manual sanity checks
504 CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
505 CheckCalculatePacketSequenceNumber(kEpoch + 1, kMask);
506 CheckCalculatePacketSequenceNumber(kEpoch, kMask);
508 // Cases where the last number was close to the start of the range
509 for (uint64 last = 0; last < 10; last++) {
510 // Small numbers should not wrap (even if they're out of order).
511 for (uint64 j = 0; j < 10; j++) {
512 CheckCalculatePacketSequenceNumber(j, last);
515 // Large numbers should not wrap either (because we're near 0 already).
516 for (uint64 j = 0; j < 10; j++) {
517 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
522 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochEnd) {
523 // Cases where the last number was close to the end of the range
524 for (uint64 i = 0; i < 10; i++) {
525 QuicPacketSequenceNumber last = kEpoch - i;
527 // Small numbers should wrap.
528 for (uint64 j = 0; j < 10; j++) {
529 CheckCalculatePacketSequenceNumber(kEpoch + j, last);
532 // Large numbers should not (even if they're out of order).
533 for (uint64 j = 0; j < 10; j++) {
534 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
539 // Next check where we're in a non-zero epoch to verify we handle
540 // reverse wrapping, too.
541 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearPrevEpoch) {
542 const uint64 prev_epoch = 1 * kEpoch;
543 const uint64 cur_epoch = 2 * kEpoch;
544 // Cases where the last number was close to the start of the range
545 for (uint64 i = 0; i < 10; i++) {
546 uint64 last = cur_epoch + i;
547 // Small number should not wrap (even if they're out of order).
548 for (uint64 j = 0; j < 10; j++) {
549 CheckCalculatePacketSequenceNumber(cur_epoch + j, last);
552 // But large numbers should reverse wrap.
553 for (uint64 j = 0; j < 10; j++) {
554 uint64 num = kEpoch - 1 - j;
555 CheckCalculatePacketSequenceNumber(prev_epoch + num, last);
560 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextEpoch) {
561 const uint64 cur_epoch = 2 * kEpoch;
562 const uint64 next_epoch = 3 * kEpoch;
563 // Cases where the last number was close to the end of the range
564 for (uint64 i = 0; i < 10; i++) {
565 QuicPacketSequenceNumber last = next_epoch - 1 - i;
567 // Small numbers should wrap.
568 for (uint64 j = 0; j < 10; j++) {
569 CheckCalculatePacketSequenceNumber(next_epoch + j, last);
572 // but large numbers should not (even if they're out of order).
573 for (uint64 j = 0; j < 10; j++) {
574 uint64 num = kEpoch - 1 - j;
575 CheckCalculatePacketSequenceNumber(cur_epoch + num, last);
580 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
581 const uint64 max_number = numeric_limits<uint64>::max();
582 const uint64 max_epoch = max_number & ~kMask;
584 // Cases where the last number was close to the end of the range
585 for (uint64 i = 0; i < 10; i++) {
586 // Subtract 1, because the expected next sequence number is 1 more than the
587 // last sequence number.
588 QuicPacketSequenceNumber last = max_number - i - 1;
590 // Small numbers should not wrap, because they have nowhere to go.
591 for (uint64 j = 0; j < 10; j++) {
592 CheckCalculatePacketSequenceNumber(max_epoch + j, last);
595 // Large numbers should not wrap either.
596 for (uint64 j = 0; j < 10; j++) {
597 uint64 num = kEpoch - 1 - j;
598 CheckCalculatePacketSequenceNumber(max_epoch + num, last);
603 TEST_P(QuicFramerTest, EmptyPacket) {
604 char packet[] = { 0x00 };
605 QuicEncryptedPacket encrypted(packet, 0, false);
606 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
607 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
610 TEST_P(QuicFramerTest, LargePacket) {
611 unsigned char packet[kMaxPacketSize + 1] = {
612 // public flags (8 byte connection_id)
613 0x3C,
614 // connection_id
615 0x10,
616 0x32,
617 0x54,
618 0x76,
619 0x98,
620 0xBA,
621 0xDC,
622 0xFE,
623 // packet sequence number
624 0xBC,
625 0x9A,
626 0x78,
627 0x56,
628 0x34,
629 0x12,
630 // private flags
631 0x00,
634 memset(packet + GetPacketHeaderSize(
635 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
636 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP), 0,
637 kMaxPacketSize - GetPacketHeaderSize(
638 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
639 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP) + 1);
641 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
642 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
644 ASSERT_TRUE(visitor_.header_.get());
645 // Make sure we've parsed the packet header, so we can send an error.
646 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
647 visitor_.header_->public_header.connection_id);
648 // Make sure the correct error is propagated.
649 EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
652 TEST_P(QuicFramerTest, PacketHeader) {
653 unsigned char packet[] = {
654 // public flags (8 byte connection_id)
655 0x3C,
656 // connection_id
657 0x10, 0x32, 0x54, 0x76,
658 0x98, 0xBA, 0xDC, 0xFE,
659 // packet sequence number
660 0xBC, 0x9A, 0x78, 0x56,
661 0x34, 0x12,
662 // private flags
663 0x00,
666 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
667 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
668 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
669 ASSERT_TRUE(visitor_.header_.get());
670 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
671 visitor_.header_->public_header.connection_id);
672 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
673 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
674 EXPECT_FALSE(visitor_.header_->fec_flag);
675 EXPECT_FALSE(visitor_.header_->entropy_flag);
676 EXPECT_EQ(0, visitor_.header_->entropy_hash);
677 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
678 visitor_.header_->packet_sequence_number);
679 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
680 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
682 // Now test framing boundaries.
683 for (size_t i = 0;
684 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
685 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
686 ++i) {
687 string expected_error;
688 if (i < kConnectionIdOffset) {
689 expected_error = "Unable to read public flags.";
690 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
691 expected_error = "Unable to read ConnectionId.";
692 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion)) {
693 expected_error = "Unable to read sequence number.";
694 } else if (i < GetFecGroupOffset(!kIncludeVersion)) {
695 expected_error = "Unable to read private flags.";
696 } else {
697 expected_error = "Unable to read first fec protected packet offset.";
699 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
703 TEST_P(QuicFramerTest, PacketHeaderWith4ByteConnectionId) {
704 QuicFramerPeer::SetLastSerializedConnectionId(
705 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
707 unsigned char packet[] = {
708 // public flags (4 byte connection_id)
709 0x38,
710 // connection_id
711 0x10, 0x32, 0x54, 0x76,
712 // packet sequence number
713 0xBC, 0x9A, 0x78, 0x56,
714 0x34, 0x12,
715 // private flags
716 0x00,
719 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
720 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
721 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
722 ASSERT_TRUE(visitor_.header_.get());
723 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
724 visitor_.header_->public_header.connection_id);
725 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
726 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
727 EXPECT_FALSE(visitor_.header_->fec_flag);
728 EXPECT_FALSE(visitor_.header_->entropy_flag);
729 EXPECT_EQ(0, visitor_.header_->entropy_hash);
730 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
731 visitor_.header_->packet_sequence_number);
732 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
733 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
735 // Now test framing boundaries.
736 for (size_t i = 0;
737 i < GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID, !kIncludeVersion,
738 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
739 ++i) {
740 string expected_error;
741 if (i < kConnectionIdOffset) {
742 expected_error = "Unable to read public flags.";
743 } else if (i < GetSequenceNumberOffset(PACKET_4BYTE_CONNECTION_ID,
744 !kIncludeVersion)) {
745 expected_error = "Unable to read ConnectionId.";
746 } else if (i < GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID,
747 !kIncludeVersion)) {
748 expected_error = "Unable to read sequence number.";
749 } else if (i < GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID,
750 !kIncludeVersion)) {
751 expected_error = "Unable to read private flags.";
752 } else {
753 expected_error = "Unable to read first fec protected packet offset.";
755 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
759 TEST_P(QuicFramerTest, PacketHeader1ByteConnectionId) {
760 QuicFramerPeer::SetLastSerializedConnectionId(
761 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
763 unsigned char packet[] = {
764 // public flags (1 byte connection_id)
765 0x34,
766 // connection_id
767 0x10,
768 // packet sequence number
769 0xBC, 0x9A, 0x78, 0x56,
770 0x34, 0x12,
771 // private flags
772 0x00,
775 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
776 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
777 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
778 ASSERT_TRUE(visitor_.header_.get());
779 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
780 visitor_.header_->public_header.connection_id);
781 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
782 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
783 EXPECT_FALSE(visitor_.header_->fec_flag);
784 EXPECT_FALSE(visitor_.header_->entropy_flag);
785 EXPECT_EQ(0, visitor_.header_->entropy_hash);
786 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
787 visitor_.header_->packet_sequence_number);
788 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
789 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
791 // Now test framing boundaries.
792 for (size_t i = 0;
793 i < GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID, !kIncludeVersion,
794 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
795 ++i) {
796 string expected_error;
797 if (i < kConnectionIdOffset) {
798 expected_error = "Unable to read public flags.";
799 } else if (i < GetSequenceNumberOffset(PACKET_1BYTE_CONNECTION_ID,
800 !kIncludeVersion)) {
801 expected_error = "Unable to read ConnectionId.";
802 } else if (i < GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID,
803 !kIncludeVersion)) {
804 expected_error = "Unable to read sequence number.";
805 } else if (i < GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID,
806 !kIncludeVersion)) {
807 expected_error = "Unable to read private flags.";
808 } else {
809 expected_error = "Unable to read first fec protected packet offset.";
811 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
815 TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
816 QuicFramerPeer::SetLastSerializedConnectionId(
817 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
819 unsigned char packet[] = {
820 // public flags (0 byte connection_id)
821 0x30,
822 // connection_id
823 // packet sequence number
824 0xBC, 0x9A, 0x78, 0x56,
825 0x34, 0x12,
826 // private flags
827 0x00,
830 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
831 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
832 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
833 ASSERT_TRUE(visitor_.header_.get());
834 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
835 visitor_.header_->public_header.connection_id);
836 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
837 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
838 EXPECT_FALSE(visitor_.header_->fec_flag);
839 EXPECT_FALSE(visitor_.header_->entropy_flag);
840 EXPECT_EQ(0, visitor_.header_->entropy_hash);
841 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
842 visitor_.header_->packet_sequence_number);
843 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
844 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
846 // Now test framing boundaries.
847 for (size_t i = 0;
848 i < GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
849 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
850 ++i) {
851 string expected_error;
852 if (i < kConnectionIdOffset) {
853 expected_error = "Unable to read public flags.";
854 } else if (i < GetSequenceNumberOffset(PACKET_0BYTE_CONNECTION_ID,
855 !kIncludeVersion)) {
856 expected_error = "Unable to read ConnectionId.";
857 } else if (i < GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID,
858 !kIncludeVersion)) {
859 expected_error = "Unable to read sequence number.";
860 } else if (i < GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID,
861 !kIncludeVersion)) {
862 expected_error = "Unable to read private flags.";
863 } else {
864 expected_error = "Unable to read first fec protected packet offset.";
866 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
870 TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
871 unsigned char packet[] = {
872 // public flags (version)
873 0x3D,
874 // connection_id
875 0x10, 0x32, 0x54, 0x76,
876 0x98, 0xBA, 0xDC, 0xFE,
877 // version tag
878 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
879 // packet sequence number
880 0xBC, 0x9A, 0x78, 0x56,
881 0x34, 0x12,
882 // private flags
883 0x00,
886 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
887 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
888 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
889 ASSERT_TRUE(visitor_.header_.get());
890 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
891 visitor_.header_->public_header.connection_id);
892 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
893 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
894 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
895 EXPECT_FALSE(visitor_.header_->fec_flag);
896 EXPECT_FALSE(visitor_.header_->entropy_flag);
897 EXPECT_EQ(0, visitor_.header_->entropy_hash);
898 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
899 visitor_.header_->packet_sequence_number);
900 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
901 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
903 // Now test framing boundaries.
904 for (size_t i = 0;
905 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
906 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
907 ++i) {
908 string expected_error;
909 if (i < kConnectionIdOffset) {
910 expected_error = "Unable to read public flags.";
911 } else if (i < kVersionOffset) {
912 expected_error = "Unable to read ConnectionId.";
913 } else if (i < GetSequenceNumberOffset(kIncludeVersion)) {
914 expected_error = "Unable to read protocol version.";
915 } else if (i < GetPrivateFlagsOffset(kIncludeVersion)) {
916 expected_error = "Unable to read sequence number.";
917 } else if (i < GetFecGroupOffset(kIncludeVersion)) {
918 expected_error = "Unable to read private flags.";
919 } else {
920 expected_error = "Unable to read first fec protected packet offset.";
922 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
926 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
927 QuicFramerPeer::SetLastSequenceNumber(&framer_,
928 GG_UINT64_C(0x123456789ABA));
930 unsigned char packet[] = {
931 // public flags (8 byte connection_id and 4 byte sequence number)
932 0x2C,
933 // connection_id
934 0x10, 0x32, 0x54, 0x76,
935 0x98, 0xBA, 0xDC, 0xFE,
936 // packet sequence number
937 0xBC, 0x9A, 0x78, 0x56,
938 // private flags
939 0x00,
942 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
943 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
944 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
945 ASSERT_TRUE(visitor_.header_.get());
946 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
947 visitor_.header_->public_header.connection_id);
948 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
949 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
950 EXPECT_FALSE(visitor_.header_->fec_flag);
951 EXPECT_FALSE(visitor_.header_->entropy_flag);
952 EXPECT_EQ(0, visitor_.header_->entropy_hash);
953 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
954 visitor_.header_->packet_sequence_number);
955 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
956 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
958 // Now test framing boundaries.
959 for (size_t i = 0;
960 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
961 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
962 ++i) {
963 string expected_error;
964 if (i < kConnectionIdOffset) {
965 expected_error = "Unable to read public flags.";
966 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
967 expected_error = "Unable to read ConnectionId.";
968 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
969 PACKET_4BYTE_SEQUENCE_NUMBER)) {
970 expected_error = "Unable to read sequence number.";
971 } else if (i < GetFecGroupOffset(!kIncludeVersion,
972 PACKET_4BYTE_SEQUENCE_NUMBER)) {
973 expected_error = "Unable to read private flags.";
974 } else {
975 expected_error = "Unable to read first fec protected packet offset.";
977 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
981 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
982 QuicFramerPeer::SetLastSequenceNumber(&framer_,
983 GG_UINT64_C(0x123456789ABA));
985 unsigned char packet[] = {
986 // public flags (8 byte connection_id and 2 byte sequence number)
987 0x1C,
988 // connection_id
989 0x10, 0x32, 0x54, 0x76,
990 0x98, 0xBA, 0xDC, 0xFE,
991 // packet sequence number
992 0xBC, 0x9A,
993 // private flags
994 0x00,
997 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
998 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
999 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1000 ASSERT_TRUE(visitor_.header_.get());
1001 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1002 visitor_.header_->public_header.connection_id);
1003 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1004 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1005 EXPECT_FALSE(visitor_.header_->fec_flag);
1006 EXPECT_FALSE(visitor_.header_->entropy_flag);
1007 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1008 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1009 visitor_.header_->packet_sequence_number);
1010 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1011 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1013 // Now test framing boundaries.
1014 for (size_t i = 0;
1015 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1016 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
1017 ++i) {
1018 string expected_error;
1019 if (i < kConnectionIdOffset) {
1020 expected_error = "Unable to read public flags.";
1021 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
1022 expected_error = "Unable to read ConnectionId.";
1023 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1024 PACKET_2BYTE_SEQUENCE_NUMBER)) {
1025 expected_error = "Unable to read sequence number.";
1026 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1027 PACKET_2BYTE_SEQUENCE_NUMBER)) {
1028 expected_error = "Unable to read private flags.";
1029 } else {
1030 expected_error = "Unable to read first fec protected packet offset.";
1032 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1036 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
1037 QuicFramerPeer::SetLastSequenceNumber(&framer_,
1038 GG_UINT64_C(0x123456789ABA));
1040 unsigned char packet[] = {
1041 // public flags (8 byte connection_id and 1 byte sequence number)
1042 0x0C,
1043 // connection_id
1044 0x10, 0x32, 0x54, 0x76,
1045 0x98, 0xBA, 0xDC, 0xFE,
1046 // packet sequence number
1047 0xBC,
1048 // private flags
1049 0x00,
1052 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1053 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
1054 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1055 ASSERT_TRUE(visitor_.header_.get());
1056 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1057 visitor_.header_->public_header.connection_id);
1058 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1059 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1060 EXPECT_FALSE(visitor_.header_->fec_flag);
1061 EXPECT_FALSE(visitor_.header_->entropy_flag);
1062 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1063 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1064 visitor_.header_->packet_sequence_number);
1065 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1066 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1068 // Now test framing boundaries.
1069 for (size_t i = 0;
1070 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1071 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
1072 ++i) {
1073 string expected_error;
1074 if (i < kConnectionIdOffset) {
1075 expected_error = "Unable to read public flags.";
1076 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
1077 expected_error = "Unable to read ConnectionId.";
1078 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1079 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1080 expected_error = "Unable to read sequence number.";
1081 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1082 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1083 expected_error = "Unable to read private flags.";
1084 } else {
1085 expected_error = "Unable to read first fec protected packet offset.";
1087 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1091 TEST_P(QuicFramerTest, InvalidPublicFlag) {
1092 unsigned char packet[] = {
1093 // public flags: all flags set but the public reset flag and version flag.
1094 0xFC,
1095 // connection_id
1096 0x10, 0x32, 0x54, 0x76,
1097 0x98, 0xBA, 0xDC, 0xFE,
1098 // packet sequence number
1099 0xBC, 0x9A, 0x78, 0x56,
1100 0x34, 0x12,
1101 // private flags
1102 0x00,
1104 // frame type (padding)
1105 0x00,
1106 0x00, 0x00, 0x00, 0x00
1108 CheckProcessingFails(packet,
1109 arraysize(packet),
1110 "Illegal public flags value.",
1111 QUIC_INVALID_PACKET_HEADER);
1113 // Now turn off validation.
1114 framer_.set_validate_flags(false);
1115 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1116 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1119 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
1120 unsigned char packet[] = {
1121 // public flags (8 byte connection_id and version flag and an unknown flag)
1122 0x4D,
1123 // connection_id
1124 0x10, 0x32, 0x54, 0x76,
1125 0x98, 0xBA, 0xDC, 0xFE,
1126 // version tag
1127 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1128 // packet sequence number
1129 0xBC, 0x9A, 0x78, 0x56,
1130 0x34, 0x12,
1131 // private flags
1132 0x00,
1134 // frame type (padding)
1135 0x00,
1136 0x00, 0x00, 0x00, 0x00
1138 CheckProcessingFails(packet,
1139 arraysize(packet),
1140 "Illegal public flags value.",
1141 QUIC_INVALID_PACKET_HEADER);
1144 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1145 unsigned char packet[] = {
1146 // public flags (8 byte connection_id, version flag and an unknown flag)
1147 0x7D,
1148 // connection_id
1149 0x10, 0x32, 0x54, 0x76,
1150 0x98, 0xBA, 0xDC, 0xFE,
1151 // version tag
1152 'Q', '0', '0', '0',
1153 // packet sequence number
1154 0xBC, 0x9A, 0x78, 0x56,
1155 0x34, 0x12,
1156 // private flags
1157 0x00,
1159 // frame type (padding frame)
1160 0x00,
1161 0x00, 0x00, 0x00, 0x00
1163 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1164 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1165 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1166 ASSERT_TRUE(visitor_.header_.get());
1167 EXPECT_EQ(0, visitor_.frame_count_);
1168 EXPECT_EQ(1, visitor_.version_mismatch_);
1171 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
1172 unsigned char packet[] = {
1173 // public flags (8 byte connection_id)
1174 0x3C,
1175 // connection_id
1176 0x10, 0x32, 0x54, 0x76,
1177 0x98, 0xBA, 0xDC, 0xFE,
1178 // packet sequence number
1179 0xBC, 0x9A, 0x78, 0x56,
1180 0x34, 0x12,
1181 // private flags
1182 0x10,
1184 // frame type (padding)
1185 0x00,
1186 0x00, 0x00, 0x00, 0x00
1188 CheckProcessingFails(packet,
1189 arraysize(packet),
1190 "Illegal private flags value.",
1191 QUIC_INVALID_PACKET_HEADER);
1194 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
1195 unsigned char packet[] = {
1196 // public flags (8 byte connection_id)
1197 0x3C,
1198 // connection_id
1199 0x10, 0x32, 0x54, 0x76,
1200 0x98, 0xBA, 0xDC, 0xFE,
1201 // packet sequence number
1202 0x01, 0x00, 0x00, 0x00,
1203 0x00, 0x00,
1204 // private flags (fec group)
1205 0x02,
1206 // first fec protected packet offset
1207 0x10
1209 CheckProcessingFails(packet,
1210 arraysize(packet),
1211 "First fec protected packet offset must be less "
1212 "than the sequence number.",
1213 QUIC_INVALID_PACKET_HEADER);
1216 TEST_P(QuicFramerTest, PaddingFrame) {
1217 unsigned char packet[] = {
1218 // public flags (8 byte connection_id)
1219 0x3C,
1220 // connection_id
1221 0x10, 0x32, 0x54, 0x76,
1222 0x98, 0xBA, 0xDC, 0xFE,
1223 // packet sequence number
1224 0xBC, 0x9A, 0x78, 0x56,
1225 0x34, 0x12,
1226 // private flags
1227 0x00,
1229 // frame type (padding frame)
1230 0x00,
1231 // Ignored data (which in this case is a stream frame)
1232 // frame type (stream frame with fin)
1233 0xFF,
1234 // stream id
1235 0x04, 0x03, 0x02, 0x01,
1236 // offset
1237 0x54, 0x76, 0x10, 0x32,
1238 0xDC, 0xFE, 0x98, 0xBA,
1239 // data length
1240 0x0c, 0x00,
1241 // data
1242 'h', 'e', 'l', 'l',
1243 'o', ' ', 'w', 'o',
1244 'r', 'l', 'd', '!',
1247 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1248 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1249 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1250 ASSERT_TRUE(visitor_.header_.get());
1251 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1253 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1254 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1255 // A packet with no frames is not acceptable.
1256 CheckProcessingFails(
1257 packet,
1258 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1259 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1260 "Packet has no frames.", QUIC_MISSING_PAYLOAD);
1263 TEST_P(QuicFramerTest, StreamFrame) {
1264 unsigned char packet[] = {
1265 // public flags (8 byte connection_id)
1266 0x3C,
1267 // connection_id
1268 0x10, 0x32, 0x54, 0x76,
1269 0x98, 0xBA, 0xDC, 0xFE,
1270 // packet sequence number
1271 0xBC, 0x9A, 0x78, 0x56,
1272 0x34, 0x12,
1273 // private flags
1274 0x00,
1276 // frame type (stream frame with fin)
1277 0xFF,
1278 // stream id
1279 0x04, 0x03, 0x02, 0x01,
1280 // offset
1281 0x54, 0x76, 0x10, 0x32,
1282 0xDC, 0xFE, 0x98, 0xBA,
1283 // data length
1284 0x0c, 0x00,
1285 // data
1286 'h', 'e', 'l', 'l',
1287 'o', ' ', 'w', 'o',
1288 'r', 'l', 'd', '!',
1291 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1292 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1294 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1295 ASSERT_TRUE(visitor_.header_.get());
1296 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1298 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1299 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1300 EXPECT_EQ(static_cast<uint64>(0x01020304),
1301 visitor_.stream_frames_[0]->stream_id);
1302 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1303 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1304 visitor_.stream_frames_[0]->offset);
1305 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1307 // Now test framing boundaries.
1308 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
1311 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
1312 unsigned char packet[] = {
1313 // public flags (8 byte connection_id)
1314 0x3C,
1315 // connection_id
1316 0x10, 0x32, 0x54, 0x76,
1317 0x98, 0xBA, 0xDC, 0xFE,
1318 // packet sequence number
1319 0xBC, 0x9A, 0x78, 0x56,
1320 0x34, 0x12,
1321 // private flags
1322 0x00,
1324 // frame type (stream frame with fin)
1325 0xFE,
1326 // stream id
1327 0x04, 0x03, 0x02,
1328 // offset
1329 0x54, 0x76, 0x10, 0x32,
1330 0xDC, 0xFE, 0x98, 0xBA,
1331 // data length
1332 0x0c, 0x00,
1333 // data
1334 'h', 'e', 'l', 'l',
1335 'o', ' ', 'w', 'o',
1336 'r', 'l', 'd', '!',
1339 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1340 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1342 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1343 ASSERT_TRUE(visitor_.header_.get());
1344 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1346 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1347 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1348 EXPECT_EQ(GG_UINT64_C(0x00020304), visitor_.stream_frames_[0]->stream_id);
1349 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1350 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1351 visitor_.stream_frames_[0]->offset);
1352 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1354 // Now test framing boundaries.
1355 const size_t stream_id_size = 3;
1356 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1359 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
1360 unsigned char packet[] = {
1361 // public flags (8 byte connection_id)
1362 0x3C,
1363 // connection_id
1364 0x10, 0x32, 0x54, 0x76,
1365 0x98, 0xBA, 0xDC, 0xFE,
1366 // packet sequence number
1367 0xBC, 0x9A, 0x78, 0x56,
1368 0x34, 0x12,
1369 // private flags
1370 0x00,
1372 // frame type (stream frame with fin)
1373 0xFD,
1374 // stream id
1375 0x04, 0x03,
1376 // offset
1377 0x54, 0x76, 0x10, 0x32,
1378 0xDC, 0xFE, 0x98, 0xBA,
1379 // data length
1380 0x0c, 0x00,
1381 // data
1382 'h', 'e', 'l', 'l',
1383 'o', ' ', 'w', 'o',
1384 'r', 'l', 'd', '!',
1387 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1388 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1390 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1391 ASSERT_TRUE(visitor_.header_.get());
1392 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1394 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1395 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1396 EXPECT_EQ(static_cast<uint64>(0x00000304),
1397 visitor_.stream_frames_[0]->stream_id);
1398 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1399 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1400 visitor_.stream_frames_[0]->offset);
1401 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1403 // Now test framing boundaries.
1404 const size_t stream_id_size = 2;
1405 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1408 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
1409 unsigned char packet[] = {
1410 // public flags (8 byte connection_id)
1411 0x3C,
1412 // connection_id
1413 0x10, 0x32, 0x54, 0x76,
1414 0x98, 0xBA, 0xDC, 0xFE,
1415 // packet sequence number
1416 0xBC, 0x9A, 0x78, 0x56,
1417 0x34, 0x12,
1418 // private flags
1419 0x00,
1421 // frame type (stream frame with fin)
1422 0xFC,
1423 // stream id
1424 0x04,
1425 // offset
1426 0x54, 0x76, 0x10, 0x32,
1427 0xDC, 0xFE, 0x98, 0xBA,
1428 // data length
1429 0x0c, 0x00,
1430 // data
1431 'h', 'e', 'l', 'l',
1432 'o', ' ', 'w', 'o',
1433 'r', 'l', 'd', '!',
1436 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1437 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1439 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1440 ASSERT_TRUE(visitor_.header_.get());
1441 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1443 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1444 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1445 EXPECT_EQ(static_cast<uint64>(0x00000004),
1446 visitor_.stream_frames_[0]->stream_id);
1447 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1448 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1449 visitor_.stream_frames_[0]->offset);
1450 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1452 // Now test framing boundaries.
1453 const size_t stream_id_size = 1;
1454 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1457 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
1458 unsigned char packet[] = {
1459 // public flags (version, 8 byte connection_id)
1460 0x3D,
1461 // connection_id
1462 0x10, 0x32, 0x54, 0x76,
1463 0x98, 0xBA, 0xDC, 0xFE,
1464 // version tag
1465 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1466 // packet sequence number
1467 0xBC, 0x9A, 0x78, 0x56,
1468 0x34, 0x12,
1469 // private flags
1470 0x00,
1472 // frame type (stream frame with fin)
1473 0xFF,
1474 // stream id
1475 0x04, 0x03, 0x02, 0x01,
1476 // offset
1477 0x54, 0x76, 0x10, 0x32,
1478 0xDC, 0xFE, 0x98, 0xBA,
1479 // data length
1480 0x0c, 0x00,
1481 // data
1482 'h', 'e', 'l', 'l',
1483 'o', ' ', 'w', 'o',
1484 'r', 'l', 'd', '!',
1487 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1488 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1490 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1491 ASSERT_TRUE(visitor_.header_.get());
1492 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
1493 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
1494 EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
1496 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1497 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1498 EXPECT_EQ(static_cast<uint64>(0x01020304),
1499 visitor_.stream_frames_[0]->stream_id);
1500 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1501 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1502 visitor_.stream_frames_[0]->offset);
1503 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1505 // Now test framing boundaries.
1506 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
1509 TEST_P(QuicFramerTest, RejectPacket) {
1510 visitor_.accept_packet_ = false;
1512 unsigned char packet[] = {
1513 // public flags (8 byte connection_id)
1514 0x3C,
1515 // connection_id
1516 0x10, 0x32, 0x54, 0x76,
1517 0x98, 0xBA, 0xDC, 0xFE,
1518 // packet sequence number
1519 0xBC, 0x9A, 0x78, 0x56,
1520 0x34, 0x12,
1521 // private flags
1522 0x00,
1524 // frame type (stream frame with fin)
1525 0xFF,
1526 // stream id
1527 0x04, 0x03, 0x02, 0x01,
1528 // offset
1529 0x54, 0x76, 0x10, 0x32,
1530 0xDC, 0xFE, 0x98, 0xBA,
1531 // data length
1532 0x0c, 0x00,
1533 // data
1534 'h', 'e', 'l', 'l',
1535 'o', ' ', 'w', 'o',
1536 'r', 'l', 'd', '!',
1539 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1540 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1542 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1543 ASSERT_TRUE(visitor_.header_.get());
1544 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1546 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1547 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1550 TEST_P(QuicFramerTest, RejectPublicHeader) {
1551 visitor_.accept_public_header_ = false;
1553 unsigned char packet[] = {
1554 // public flags (8 byte connection_id)
1555 0x3C,
1556 // connection_id
1557 0x10, 0x32, 0x54, 0x76,
1558 0x98, 0xBA, 0xDC, 0xFE,
1561 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1562 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1564 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1565 ASSERT_TRUE(visitor_.public_header_.get());
1566 ASSERT_FALSE(visitor_.header_.get());
1569 TEST_P(QuicFramerTest, RevivedStreamFrame) {
1570 unsigned char payload[] = {
1571 // frame type (stream frame with fin)
1572 0xFF,
1573 // stream id
1574 0x04, 0x03, 0x02, 0x01,
1575 // offset
1576 0x54, 0x76, 0x10, 0x32,
1577 0xDC, 0xFE, 0x98, 0xBA,
1578 // data length
1579 0x0c, 0x00,
1580 // data
1581 'h', 'e', 'l', 'l',
1582 'o', ' ', 'w', 'o',
1583 'r', 'l', 'd', '!',
1586 QuicPacketHeader header;
1587 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
1588 header.public_header.reset_flag = false;
1589 header.public_header.version_flag = false;
1590 header.fec_flag = true;
1591 header.entropy_flag = true;
1592 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
1593 header.fec_group = 0;
1595 // Do not encrypt the payload because the revived payload is post-encryption.
1596 EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
1597 StringPiece(AsChars(payload),
1598 arraysize(payload))));
1600 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1601 ASSERT_EQ(1, visitor_.revived_packets_);
1602 ASSERT_TRUE(visitor_.header_.get());
1603 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1604 visitor_.header_->public_header.connection_id);
1605 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1606 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1607 EXPECT_TRUE(visitor_.header_->fec_flag);
1608 EXPECT_TRUE(visitor_.header_->entropy_flag);
1609 EXPECT_EQ(1 << (header.packet_sequence_number % 8),
1610 visitor_.header_->entropy_hash);
1611 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1612 visitor_.header_->packet_sequence_number);
1613 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1614 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1616 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1617 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1618 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1619 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1620 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1621 visitor_.stream_frames_[0]->offset);
1622 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1625 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
1626 unsigned char packet[] = {
1627 // public flags (8 byte connection_id)
1628 0x3C,
1629 // connection_id
1630 0x10, 0x32, 0x54, 0x76,
1631 0x98, 0xBA, 0xDC, 0xFE,
1632 // packet sequence number
1633 0xBC, 0x9A, 0x78, 0x56,
1634 0x12, 0x34,
1635 // private flags (fec group)
1636 0x02,
1637 // first fec protected packet offset
1638 0x02,
1640 // frame type (stream frame with fin)
1641 0xFF,
1642 // stream id
1643 0x04, 0x03, 0x02, 0x01,
1644 // offset
1645 0x54, 0x76, 0x10, 0x32,
1646 0xDC, 0xFE, 0x98, 0xBA,
1647 // data length
1648 0x0c, 0x00,
1649 // data
1650 'h', 'e', 'l', 'l',
1651 'o', ' ', 'w', 'o',
1652 'r', 'l', 'd', '!',
1655 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1656 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1658 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1659 ASSERT_TRUE(visitor_.header_.get());
1660 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1661 EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1662 EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
1663 visitor_.header_->fec_group);
1664 const size_t fec_offset =
1665 GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID,
1666 !kIncludeVersion,
1667 PACKET_6BYTE_SEQUENCE_NUMBER);
1668 EXPECT_EQ(
1669 string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
1670 visitor_.fec_protected_payload_);
1672 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1673 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1674 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1675 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1676 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1677 visitor_.stream_frames_[0]->offset);
1678 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1681 TEST_P(QuicFramerTest, AckFrameTwoTimestamp) {
1682 unsigned char packet[] = {
1683 // public flags (8 byte connection_id)
1684 0x3C,
1685 // connection_id
1686 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1687 // packet sequence number
1688 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1689 // private flags (entropy)
1690 0x01,
1692 // frame type (ack frame)
1693 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1694 0x6C,
1695 // entropy hash of all received packets.
1696 0xBA,
1697 // largest observed packet sequence number
1698 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1699 // Zero delta time.
1700 0x00, 0x00,
1701 // Number of timestamps.
1702 0x02,
1703 // Delta from largest observed.
1704 0x01,
1705 // Delta time.
1706 0x10, 0x32, 0x54, 0x76,
1707 // Delta from largest observed.
1708 0x02,
1709 // Delta time.
1710 0x10, 0x32,
1711 // num missing packets
1712 0x01,
1713 // missing packet delta
1714 0x01,
1715 // 0 more missing packets in range.
1716 0x00,
1717 // Number of revived packets.
1718 0x00,
1721 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1722 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1724 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1725 ASSERT_TRUE(visitor_.header_.get());
1726 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1728 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1729 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1730 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1731 EXPECT_EQ(0xBA, frame.entropy_hash);
1732 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
1733 ASSERT_EQ(1u, frame.missing_packets.size());
1734 ASSERT_EQ(2u, frame.received_packet_times.size());
1735 SequenceNumberSet::const_iterator missing_iter =
1736 frame.missing_packets.begin();
1737 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1739 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1740 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1741 kQuicEntropyHashSize;
1742 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1743 PACKET_6BYTE_SEQUENCE_NUMBER;
1744 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1745 kQuicDeltaTimeLargestObservedSize;
1746 const size_t kTimestampDeltaLargestObserved1 = kNumTimestampsOffset +
1747 kQuicNumTimestampsSize;
1748 const size_t kTimestampTimeDeltaLargestObserved1 =
1749 kTimestampDeltaLargestObserved1 + 1;
1750 const size_t kTimestampDeltaLargestObserved2 =
1751 kTimestampTimeDeltaLargestObserved1 + 4;
1752 const size_t kTimestampTimeDeltaLargestObserved2 =
1753 kTimestampDeltaLargestObserved2 + 1;
1754 const size_t kNumMissingPacketOffset =
1755 kTimestampTimeDeltaLargestObserved2 + 2;
1756 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1757 kNumberOfNackRangesSize;
1758 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1759 PACKET_1BYTE_SEQUENCE_NUMBER;
1760 const size_t kRevivedPacketsLength = kMissingPacketsRange +
1761 PACKET_1BYTE_SEQUENCE_NUMBER;
1762 // Now test framing boundaries.
1763 const size_t ack_frame_size = kRevivedPacketsLength +
1764 PACKET_1BYTE_SEQUENCE_NUMBER;
1765 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1766 string expected_error;
1767 if (i < kLargestObservedOffset) {
1768 expected_error = "Unable to read entropy hash for received packets.";
1769 } else if (i < kMissingDeltaTimeOffset) {
1770 expected_error = "Unable to read largest observed.";
1771 } else if (i < kNumTimestampsOffset) {
1772 expected_error = "Unable to read delta time largest observed.";
1773 } else if (i < kTimestampDeltaLargestObserved1) {
1774 expected_error = "Unable to read num received packets.";
1775 } else if (i < kTimestampTimeDeltaLargestObserved1) {
1776 expected_error = "Unable to read sequence delta in received packets.";
1777 } else if (i < kTimestampDeltaLargestObserved2) {
1778 expected_error = "Unable to read time delta in received packets.";
1779 } else if (i < kTimestampTimeDeltaLargestObserved2) {
1780 expected_error = "Unable to read sequence delta in received packets.";
1781 } else if (i < kNumMissingPacketOffset) {
1782 expected_error =
1783 "Unable to read incremental time delta in received packets.";
1784 } else if (i < kMissingPacketsOffset) {
1785 expected_error = "Unable to read num missing packet ranges.";
1786 } else if (i < kMissingPacketsRange) {
1787 expected_error = "Unable to read missing sequence number delta.";
1788 } else if (i < kRevivedPacketsLength) {
1789 expected_error = "Unable to read missing sequence number range.";
1790 } else {
1791 expected_error = "Unable to read num revived packets.";
1793 CheckProcessingFails(
1794 packet,
1795 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1796 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1797 expected_error, QUIC_INVALID_ACK_DATA);
1802 TEST_P(QuicFramerTest, AckFrameOneTimestamp) {
1803 unsigned char packet[] = {
1804 // public flags (8 byte connection_id)
1805 0x3C,
1806 // connection_id
1807 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1808 // packet sequence number
1809 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1810 // private flags (entropy)
1811 0x01,
1813 // frame type (ack frame)
1814 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1815 0x6C,
1816 // entropy hash of all received packets.
1817 0xBA,
1818 // largest observed packet sequence number
1819 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1820 // Zero delta time.
1821 0x00, 0x00,
1822 // Number of timestamps.
1823 0x01,
1824 // Delta from largest observed.
1825 0x01,
1826 // Delta time.
1827 0x10, 0x32, 0x54, 0x76,
1828 // num missing packets
1829 0x01,
1830 // missing packet delta
1831 0x01,
1832 // 0 more missing packets in range.
1833 0x00,
1834 // Number of revived packets.
1835 0x00,
1838 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1839 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1841 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1842 ASSERT_TRUE(visitor_.header_.get());
1843 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1845 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1846 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1847 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1848 EXPECT_EQ(0xBA, frame.entropy_hash);
1849 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
1850 ASSERT_EQ(1u, frame.missing_packets.size());
1851 ASSERT_EQ(1u, frame.received_packet_times.size());
1852 SequenceNumberSet::const_iterator missing_iter =
1853 frame.missing_packets.begin();
1854 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1856 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1857 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1858 kQuicEntropyHashSize;
1859 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1860 PACKET_6BYTE_SEQUENCE_NUMBER;
1861 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1862 kQuicDeltaTimeLargestObservedSize;
1863 const size_t kTimestampDeltaLargestObserved = kNumTimestampsOffset +
1864 kQuicNumTimestampsSize;
1865 const size_t kTimestampTimeDeltaLargestObserved =
1866 kTimestampDeltaLargestObserved + 1;
1867 const size_t kNumMissingPacketOffset = kTimestampTimeDeltaLargestObserved + 4;
1868 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1869 kNumberOfNackRangesSize;
1870 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1871 PACKET_1BYTE_SEQUENCE_NUMBER;
1872 const size_t kRevivedPacketsLength = kMissingPacketsRange +
1873 PACKET_1BYTE_SEQUENCE_NUMBER;
1874 // Now test framing boundaries.
1875 const size_t ack_frame_size = kRevivedPacketsLength +
1876 PACKET_1BYTE_SEQUENCE_NUMBER;
1877 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1878 string expected_error;
1879 if (i < kLargestObservedOffset) {
1880 expected_error = "Unable to read entropy hash for received packets.";
1881 } else if (i < kMissingDeltaTimeOffset) {
1882 expected_error = "Unable to read largest observed.";
1883 } else if (i < kNumTimestampsOffset) {
1884 expected_error = "Unable to read delta time largest observed.";
1885 } else if (i < kTimestampDeltaLargestObserved) {
1886 expected_error = "Unable to read num received packets.";
1887 } else if (i < kTimestampTimeDeltaLargestObserved) {
1888 expected_error = "Unable to read sequence delta in received packets.";
1889 } else if (i < kNumMissingPacketOffset) {
1890 expected_error = "Unable to read time delta in received packets.";
1891 } else if (i < kMissingPacketsOffset) {
1892 expected_error = "Unable to read num missing packet ranges.";
1893 } else if (i < kMissingPacketsRange) {
1894 expected_error = "Unable to read missing sequence number delta.";
1895 } else if (i < kRevivedPacketsLength) {
1896 expected_error = "Unable to read missing sequence number range.";
1897 } else {
1898 expected_error = "Unable to read num revived packets.";
1900 CheckProcessingFails(
1901 packet,
1902 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1903 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1904 expected_error, QUIC_INVALID_ACK_DATA);
1909 TEST_P(QuicFramerTest, AckFrame) {
1910 unsigned char packet[] = {
1911 // public flags (8 byte connection_id)
1912 0x3C,
1913 // connection_id
1914 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1915 // packet sequence number
1916 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1917 // private flags (entropy)
1918 0x01,
1920 // frame type (ack frame)
1921 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1922 0x6C,
1923 // entropy hash of all received packets.
1924 0xBA,
1925 // largest observed packet sequence number
1926 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1927 // Zero delta time.
1928 0x00, 0x00,
1929 // Number of timestamps.
1930 0x00,
1931 // num missing packets
1932 0x01,
1933 // missing packet delta
1934 0x01,
1935 // 0 more missing packets in range.
1936 0x00,
1937 // Number of revived packets.
1938 0x00,
1941 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1942 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1944 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1945 ASSERT_TRUE(visitor_.header_.get());
1946 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1948 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1949 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1950 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1951 EXPECT_EQ(0xBA, frame.entropy_hash);
1952 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
1953 ASSERT_EQ(1u, frame.missing_packets.size());
1954 SequenceNumberSet::const_iterator missing_iter =
1955 frame.missing_packets.begin();
1956 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1958 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1959 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1960 kQuicEntropyHashSize;
1961 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1962 PACKET_6BYTE_SEQUENCE_NUMBER;
1963 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1964 kQuicDeltaTimeLargestObservedSize;
1965 const size_t kNumMissingPacketOffset = kNumTimestampsOffset +
1966 kQuicNumTimestampsSize;
1967 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1968 kNumberOfNackRangesSize;
1969 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1970 PACKET_1BYTE_SEQUENCE_NUMBER;
1971 const size_t kRevivedPacketsLength = kMissingPacketsRange +
1972 PACKET_1BYTE_SEQUENCE_NUMBER;
1973 // Now test framing boundaries.
1974 const size_t ack_frame_size = kRevivedPacketsLength +
1975 PACKET_1BYTE_SEQUENCE_NUMBER;
1976 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1977 string expected_error;
1978 if (i < kLargestObservedOffset) {
1979 expected_error = "Unable to read entropy hash for received packets.";
1980 } else if (i < kMissingDeltaTimeOffset) {
1981 expected_error = "Unable to read largest observed.";
1982 } else if (i < kNumTimestampsOffset) {
1983 expected_error = "Unable to read delta time largest observed.";
1984 } else if (i < kNumMissingPacketOffset) {
1985 expected_error = "Unable to read num received packets.";
1986 } else if (i < kMissingPacketsOffset) {
1987 expected_error = "Unable to read num missing packet ranges.";
1988 } else if (i < kMissingPacketsRange) {
1989 expected_error = "Unable to read missing sequence number delta.";
1990 } else if (i < kRevivedPacketsLength) {
1991 expected_error = "Unable to read missing sequence number range.";
1992 } else {
1993 expected_error = "Unable to read num revived packets.";
1995 CheckProcessingFails(
1996 packet,
1997 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1998 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1999 expected_error, QUIC_INVALID_ACK_DATA);
2003 TEST_P(QuicFramerTest, AckFrameRevivedPackets) {
2004 unsigned char packet[] = {
2005 // public flags (8 byte connection_id)
2006 0x3C,
2007 // connection_id
2008 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2009 // packet sequence number
2010 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2011 // private flags (entropy)
2012 0x01,
2014 // frame type (ack frame)
2015 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2016 0x6C,
2017 // entropy hash of all received packets.
2018 0xBA,
2019 // largest observed packet sequence number
2020 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2021 // Zero delta time.
2022 0x00, 0x00,
2023 // num received packets.
2024 0x00,
2025 // num missing packets
2026 0x01,
2027 // missing packet delta
2028 0x01,
2029 // 0 more missing packets in range.
2030 0x00,
2031 // Number of revived packets.
2032 0x01,
2033 // Revived packet sequence number.
2034 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
2035 // Number of revived packets.
2036 0x00,
2039 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2040 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2042 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2043 ASSERT_TRUE(visitor_.header_.get());
2044 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2046 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2047 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2048 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
2049 EXPECT_EQ(0xBA, frame.entropy_hash);
2050 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
2051 ASSERT_EQ(1u, frame.missing_packets.size());
2052 SequenceNumberSet::const_iterator missing_iter =
2053 frame.missing_packets.begin();
2054 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
2056 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
2057 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
2058 kQuicEntropyHashSize;
2059 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
2060 PACKET_6BYTE_SEQUENCE_NUMBER;
2061 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
2062 kQuicDeltaTimeLargestObservedSize;
2063 const size_t kNumMissingPacketOffset = kNumTimestampsOffset +
2064 kQuicNumTimestampsSize;
2065 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
2066 kNumberOfNackRangesSize;
2067 const size_t kMissingPacketsRange = kMissingPacketsOffset +
2068 PACKET_1BYTE_SEQUENCE_NUMBER;
2069 const size_t kRevivedPacketsLength = kMissingPacketsRange +
2070 PACKET_1BYTE_SEQUENCE_NUMBER;
2071 const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
2072 PACKET_1BYTE_SEQUENCE_NUMBER;
2073 // Now test framing boundaries.
2074 const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
2075 PACKET_6BYTE_SEQUENCE_NUMBER;
2076 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
2077 string expected_error;
2078 if (i < kReceivedEntropyOffset) {
2079 expected_error = "Unable to read least unacked delta.";
2080 } else if (i < kLargestObservedOffset) {
2081 expected_error = "Unable to read entropy hash for received packets.";
2082 } else if (i < kMissingDeltaTimeOffset) {
2083 expected_error = "Unable to read largest observed.";
2084 } else if (i < kNumTimestampsOffset) {
2085 expected_error = "Unable to read delta time largest observed.";
2086 } else if (i < kNumMissingPacketOffset) {
2087 expected_error = "Unable to read num received packets.";
2088 } else if (i < kMissingPacketsOffset) {
2089 expected_error = "Unable to read num missing packet ranges.";
2090 } else if (i < kMissingPacketsRange) {
2091 expected_error = "Unable to read missing sequence number delta.";
2092 } else if (i < kRevivedPacketsLength) {
2093 expected_error = "Unable to read missing sequence number range.";
2094 } else if (i < kRevivedPacketSequenceNumberLength) {
2095 expected_error = "Unable to read num revived packets.";
2096 } else {
2097 expected_error = "Unable to read revived packet.";
2099 CheckProcessingFails(
2100 packet,
2101 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2102 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2103 expected_error, QUIC_INVALID_ACK_DATA);
2107 TEST_P(QuicFramerTest, AckFrameNoNacks) {
2108 unsigned char packet[] = {
2109 // public flags (8 byte connection_id)
2110 0x3C,
2111 // connection_id
2112 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2113 // packet sequence number
2114 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2115 // private flags (entropy)
2116 0x01,
2118 // frame type (ack frame)
2119 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2120 0x4C,
2121 // entropy hash of all received packets.
2122 0xBA,
2123 // largest observed packet sequence number
2124 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2125 // Zero delta time.
2126 0x00, 0x00,
2127 // Number of received packets.
2128 0x00,
2131 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2132 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2134 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2135 ASSERT_TRUE(visitor_.header_.get());
2136 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2138 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2139 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2140 QuicAckFrame* frame = visitor_.ack_frames_[0];
2141 EXPECT_EQ(0xBA, frame->entropy_hash);
2142 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame->largest_observed);
2143 ASSERT_EQ(0u, frame->missing_packets.size());
2145 // Verify that the packet re-serializes identically.
2146 QuicFrames frames;
2147 frames.push_back(QuicFrame(frame));
2148 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
2149 ASSERT_TRUE(data != nullptr);
2151 test::CompareCharArraysWithHexError("constructed packet", data->data(),
2152 data->length(), AsChars(packet),
2153 arraysize(packet));
2156 TEST_P(QuicFramerTest, AckFrame500Nacks) {
2157 unsigned char packet[] = {
2158 // public flags (8 byte connection_id)
2159 0x3C,
2160 // connection_id
2161 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2162 // packet sequence number
2163 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2164 // private flags (entropy)
2165 0x01,
2167 // frame type (ack frame)
2168 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2169 0x6C,
2170 // entropy hash of all received packets.
2171 0xBA,
2172 // largest observed packet sequence number
2173 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2174 // Zero delta time.
2175 0x00, 0x00,
2176 // No received packets.
2177 0x00,
2178 // num missing packet ranges
2179 0x02,
2180 // missing packet delta
2181 0x01,
2182 // 243 more missing packets in range.
2183 // The ranges are listed in this order so the re-constructed packet
2184 // matches.
2185 0xF3,
2186 // No gap between ranges
2187 0x00,
2188 // 255 more missing packets in range.
2189 0xFF,
2190 // No revived packets.
2191 0x00,
2194 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2195 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2197 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2198 ASSERT_TRUE(visitor_.header_.get());
2199 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2201 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2202 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2203 QuicAckFrame* frame = visitor_.ack_frames_[0];
2204 EXPECT_EQ(0xBA, frame->entropy_hash);
2205 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame->largest_observed);
2206 EXPECT_EQ(0u, frame->revived_packets.size());
2207 ASSERT_EQ(500u, frame->missing_packets.size());
2208 SequenceNumberSet::const_iterator first_missing_iter =
2209 frame->missing_packets.begin();
2210 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
2211 SequenceNumberSet::const_reverse_iterator last_missing_iter =
2212 frame->missing_packets.rbegin();
2213 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
2215 // Verify that the packet re-serializes identically.
2216 QuicFrames frames;
2217 frames.push_back(QuicFrame(frame));
2218 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
2219 ASSERT_TRUE(data != nullptr);
2221 test::CompareCharArraysWithHexError("constructed packet",
2222 data->data(), data->length(),
2223 AsChars(packet), arraysize(packet));
2226 TEST_P(QuicFramerTest, StopWaitingFrame) {
2227 unsigned char packet[] = {
2228 // public flags (8 byte connection_id)
2229 0x3C,
2230 // connection_id
2231 0x10, 0x32, 0x54, 0x76,
2232 0x98, 0xBA, 0xDC, 0xFE,
2233 // packet sequence number
2234 0xA8, 0x9A, 0x78, 0x56,
2235 0x34, 0x12,
2236 // private flags (entropy)
2237 0x01,
2239 // frame type (ack frame)
2240 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2241 0x06,
2242 // entropy hash of sent packets till least awaiting - 1.
2243 0xAB,
2244 // least packet sequence number awaiting an ack, delta from sequence number.
2245 0x08, 0x00, 0x00, 0x00,
2246 0x00, 0x00,
2249 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2250 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2252 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2253 ASSERT_TRUE(visitor_.header_.get());
2254 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2256 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2257 ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
2258 const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
2259 EXPECT_EQ(0xAB, frame.entropy_hash);
2260 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.least_unacked);
2262 const size_t kSentEntropyOffset = kQuicFrameTypeSize;
2263 const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
2264 const size_t frame_size = 7;
2265 for (size_t i = kQuicFrameTypeSize; i < frame_size; ++i) {
2266 string expected_error;
2267 if (i < kLeastUnackedOffset) {
2268 expected_error = "Unable to read entropy hash for sent packets.";
2269 } else {
2270 expected_error = "Unable to read least unacked delta.";
2272 CheckProcessingFails(
2273 packet,
2274 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2275 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2276 expected_error, QUIC_INVALID_STOP_WAITING_DATA);
2280 TEST_P(QuicFramerTest, RstStreamFrameQuicVersion24) {
2281 if (version_ > QUIC_VERSION_24) {
2282 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2283 return;
2286 unsigned char packet[] = {
2287 // public flags (8 byte connection_id)
2288 0x3C,
2289 // connection_id
2290 0x10, 0x32, 0x54, 0x76,
2291 0x98, 0xBA, 0xDC, 0xFE,
2292 // packet sequence number
2293 0xBC, 0x9A, 0x78, 0x56,
2294 0x34, 0x12,
2295 // private flags
2296 0x00,
2298 // frame type (rst stream frame)
2299 0x01,
2300 // stream id
2301 0x04, 0x03, 0x02, 0x01,
2303 // sent byte offset
2304 0x01, 0x02, 0x03, 0x04,
2305 0x05, 0x06, 0x07, 0x08,
2307 // error code
2308 0x01, 0x00, 0x00, 0x00,
2310 // error details length
2311 0x0d, 0x00,
2312 // error details
2313 'b', 'e', 'c', 'a',
2314 'u', 's', 'e', ' ',
2315 'I', ' ', 'c', 'a',
2316 'n',
2319 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2320 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2322 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2323 ASSERT_TRUE(visitor_.header_.get());
2324 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2326 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2327 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2328 EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
2329 EXPECT_EQ(GG_UINT64_C(0x0807060504030201),
2330 visitor_.rst_stream_frame_.byte_offset);
2332 // Now test framing boundaries.
2333 for (size_t i = kQuicFrameTypeSize;
2334 i < QuicFramer::GetMinRstStreamFrameSize(); ++i) {
2335 string expected_error;
2336 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2337 expected_error = "Unable to read stream_id.";
2338 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2339 kQuicMaxStreamOffsetSize) {
2340 expected_error = "Unable to read rst stream sent byte offset.";
2341 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2342 kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
2343 expected_error = "Unable to read rst stream error code.";
2344 } else {
2345 expected_error = "Unable to read rst stream error details.";
2347 CheckProcessingFails(
2348 packet,
2349 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2350 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2351 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2355 TEST_P(QuicFramerTest, RstStreamFrameQuic) {
2356 if (version_ <= QUIC_VERSION_24) {
2357 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2358 return;
2361 // clang-format off
2362 unsigned char packet[] = {
2363 // public flags (8 byte connection_id)
2364 0x3C,
2365 // connection_id
2366 0x10, 0x32, 0x54, 0x76,
2367 0x98, 0xBA, 0xDC, 0xFE,
2368 // packet sequence number
2369 0xBC, 0x9A, 0x78, 0x56,
2370 0x34, 0x12,
2371 // private flags
2372 0x00,
2374 // frame type (rst stream frame)
2375 0x01,
2376 // stream id
2377 0x04, 0x03, 0x02, 0x01,
2379 // sent byte offset
2380 0x01, 0x02, 0x03, 0x04,
2381 0x05, 0x06, 0x07, 0x08,
2383 // error code
2384 0x01, 0x00, 0x00, 0x00,
2386 // clang-format on
2388 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2389 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2391 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2392 ASSERT_TRUE(visitor_.header_.get());
2393 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2395 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2396 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2397 EXPECT_EQ(GG_UINT64_C(0x0807060504030201),
2398 visitor_.rst_stream_frame_.byte_offset);
2400 // Now test framing boundaries.
2401 for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetRstStreamFrameSize();
2402 ++i) {
2403 string expected_error;
2404 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2405 expected_error = "Unable to read stream_id.";
2406 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2407 kQuicMaxStreamOffsetSize) {
2408 expected_error = "Unable to read rst stream sent byte offset.";
2409 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2410 kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
2411 expected_error = "Unable to read rst stream error code.";
2413 CheckProcessingFails(
2414 packet,
2415 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2416 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2417 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2421 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
2422 unsigned char packet[] = {
2423 // public flags (8 byte connection_id)
2424 0x3C,
2425 // connection_id
2426 0x10, 0x32, 0x54, 0x76,
2427 0x98, 0xBA, 0xDC, 0xFE,
2428 // packet sequence number
2429 0xBC, 0x9A, 0x78, 0x56,
2430 0x34, 0x12,
2431 // private flags
2432 0x00,
2434 // frame type (connection close frame)
2435 0x02,
2436 // error code
2437 0x11, 0x00, 0x00, 0x00,
2439 // error details length
2440 0x0d, 0x00,
2441 // error details
2442 'b', 'e', 'c', 'a',
2443 'u', 's', 'e', ' ',
2444 'I', ' ', 'c', 'a',
2445 'n',
2448 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2449 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2451 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2452 ASSERT_TRUE(visitor_.header_.get());
2453 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2455 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2457 EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
2458 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
2460 ASSERT_EQ(0u, visitor_.ack_frames_.size());
2462 // Now test framing boundaries.
2463 for (size_t i = kQuicFrameTypeSize;
2464 i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
2465 string expected_error;
2466 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2467 expected_error = "Unable to read connection close error code.";
2468 } else {
2469 expected_error = "Unable to read connection close error details.";
2471 CheckProcessingFails(
2472 packet,
2473 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2474 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2475 expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
2479 TEST_P(QuicFramerTest, GoAwayFrame) {
2480 unsigned char packet[] = {
2481 // public flags (8 byte connection_id)
2482 0x3C,
2483 // connection_id
2484 0x10, 0x32, 0x54, 0x76,
2485 0x98, 0xBA, 0xDC, 0xFE,
2486 // packet sequence number
2487 0xBC, 0x9A, 0x78, 0x56,
2488 0x34, 0x12,
2489 // private flags
2490 0x00,
2492 // frame type (go away frame)
2493 0x03,
2494 // error code
2495 0x09, 0x00, 0x00, 0x00,
2496 // stream id
2497 0x04, 0x03, 0x02, 0x01,
2498 // error details length
2499 0x0d, 0x00,
2500 // error details
2501 'b', 'e', 'c', 'a',
2502 'u', 's', 'e', ' ',
2503 'I', ' ', 'c', 'a',
2504 'n',
2507 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2508 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2510 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2511 ASSERT_TRUE(visitor_.header_.get());
2512 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2514 EXPECT_EQ(GG_UINT64_C(0x01020304),
2515 visitor_.goaway_frame_.last_good_stream_id);
2516 EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
2517 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
2519 const size_t reason_size = arraysize("because I can") - 1;
2520 // Now test framing boundaries.
2521 for (size_t i = kQuicFrameTypeSize;
2522 i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
2523 string expected_error;
2524 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2525 expected_error = "Unable to read go away error code.";
2526 } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
2527 kQuicMaxStreamIdSize) {
2528 expected_error = "Unable to read last good stream id.";
2529 } else {
2530 expected_error = "Unable to read goaway reason.";
2532 CheckProcessingFails(
2533 packet,
2534 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2535 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2536 expected_error, QUIC_INVALID_GOAWAY_DATA);
2540 TEST_P(QuicFramerTest, WindowUpdateFrame) {
2541 unsigned char packet[] = {
2542 // public flags (8 byte connection_id)
2543 0x3C,
2544 // connection_id
2545 0x10, 0x32, 0x54, 0x76,
2546 0x98, 0xBA, 0xDC, 0xFE,
2547 // packet sequence number
2548 0xBC, 0x9A, 0x78, 0x56,
2549 0x34, 0x12,
2550 // private flags
2551 0x00,
2553 // frame type (window update frame)
2554 0x04,
2555 // stream id
2556 0x04, 0x03, 0x02, 0x01,
2557 // byte offset
2558 0x05, 0x06, 0x07, 0x08,
2559 0x09, 0x0a, 0x0b, 0x0c,
2562 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2564 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2566 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2567 ASSERT_TRUE(visitor_.header_.get());
2568 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2570 EXPECT_EQ(GG_UINT64_C(0x01020304),
2571 visitor_.window_update_frame_.stream_id);
2572 EXPECT_EQ(GG_UINT64_C(0x0c0b0a0908070605),
2573 visitor_.window_update_frame_.byte_offset);
2575 // Now test framing boundaries.
2576 for (size_t i = kQuicFrameTypeSize;
2577 i < QuicFramer::GetWindowUpdateFrameSize(); ++i) {
2578 string expected_error;
2579 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2580 expected_error = "Unable to read stream_id.";
2581 } else {
2582 expected_error = "Unable to read window byte_offset.";
2584 CheckProcessingFails(
2585 packet,
2586 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2587 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2588 expected_error, QUIC_INVALID_WINDOW_UPDATE_DATA);
2592 TEST_P(QuicFramerTest, BlockedFrame) {
2593 unsigned char packet[] = {
2594 // public flags (8 byte connection_id)
2595 0x3C,
2596 // connection_id
2597 0x10, 0x32, 0x54, 0x76,
2598 0x98, 0xBA, 0xDC, 0xFE,
2599 // packet sequence number
2600 0xBC, 0x9A, 0x78, 0x56,
2601 0x34, 0x12,
2602 // private flags
2603 0x00,
2605 // frame type (blocked frame)
2606 0x05,
2607 // stream id
2608 0x04, 0x03, 0x02, 0x01,
2611 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2613 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2615 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2616 ASSERT_TRUE(visitor_.header_.get());
2617 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2619 EXPECT_EQ(GG_UINT64_C(0x01020304),
2620 visitor_.blocked_frame_.stream_id);
2622 // Now test framing boundaries.
2623 for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetBlockedFrameSize();
2624 ++i) {
2625 string expected_error = "Unable to read stream_id.";
2626 CheckProcessingFails(
2627 packet,
2628 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2629 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2630 expected_error, QUIC_INVALID_BLOCKED_DATA);
2634 TEST_P(QuicFramerTest, PingFrame) {
2635 unsigned char packet[] = {
2636 // public flags (8 byte connection_id)
2637 0x3C,
2638 // connection_id
2639 0x10, 0x32, 0x54, 0x76,
2640 0x98, 0xBA, 0xDC, 0xFE,
2641 // packet sequence number
2642 0xBC, 0x9A, 0x78, 0x56,
2643 0x34, 0x12,
2644 // private flags
2645 0x00,
2647 // frame type (ping frame)
2648 0x07,
2651 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2652 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2654 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2655 ASSERT_TRUE(visitor_.header_.get());
2656 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2658 EXPECT_EQ(1u, visitor_.ping_frames_.size());
2660 // No need to check the PING frame boundaries because it has no payload.
2663 TEST_P(QuicFramerTest, PublicResetPacket) {
2664 unsigned char packet[] = {
2665 // public flags (public reset, 8 byte connection_id)
2666 0x0E,
2667 // connection_id
2668 0x10, 0x32, 0x54, 0x76,
2669 0x98, 0xBA, 0xDC, 0xFE,
2670 // message tag (kPRST)
2671 'P', 'R', 'S', 'T',
2672 // num_entries (2) + padding
2673 0x02, 0x00, 0x00, 0x00,
2674 // tag kRNON
2675 'R', 'N', 'O', 'N',
2676 // end offset 8
2677 0x08, 0x00, 0x00, 0x00,
2678 // tag kRSEQ
2679 'R', 'S', 'E', 'Q',
2680 // end offset 16
2681 0x10, 0x00, 0x00, 0x00,
2682 // nonce proof
2683 0x89, 0x67, 0x45, 0x23,
2684 0x01, 0xEF, 0xCD, 0xAB,
2685 // rejected sequence number
2686 0xBC, 0x9A, 0x78, 0x56,
2687 0x34, 0x12, 0x00, 0x00,
2690 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2691 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2692 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2693 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2694 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2695 visitor_.public_reset_packet_->public_header.connection_id);
2696 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2697 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2698 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2699 visitor_.public_reset_packet_->nonce_proof);
2700 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2701 visitor_.public_reset_packet_->rejected_sequence_number);
2702 EXPECT_TRUE(
2703 visitor_.public_reset_packet_->client_address.address().empty());
2705 // Now test framing boundaries.
2706 for (size_t i = 0; i < arraysize(packet); ++i) {
2707 string expected_error;
2708 DVLOG(1) << "iteration: " << i;
2709 if (i < kConnectionIdOffset) {
2710 expected_error = "Unable to read public flags.";
2711 CheckProcessingFails(packet, i, expected_error,
2712 QUIC_INVALID_PACKET_HEADER);
2713 } else if (i < kPublicResetPacketMessageTagOffset) {
2714 expected_error = "Unable to read ConnectionId.";
2715 CheckProcessingFails(packet, i, expected_error,
2716 QUIC_INVALID_PACKET_HEADER);
2717 } else {
2718 expected_error = "Unable to read reset message.";
2719 CheckProcessingFails(packet, i, expected_error,
2720 QUIC_INVALID_PUBLIC_RST_PACKET);
2725 TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
2726 unsigned char packet[] = {
2727 // public flags (public reset, 8 byte connection_id)
2728 0x0E,
2729 // connection_id
2730 0x10, 0x32, 0x54, 0x76,
2731 0x98, 0xBA, 0xDC, 0xFE,
2732 // message tag (kPRST)
2733 'P', 'R', 'S', 'T',
2734 // num_entries (2) + padding
2735 0x02, 0x00, 0x00, 0x00,
2736 // tag kRNON
2737 'R', 'N', 'O', 'N',
2738 // end offset 8
2739 0x08, 0x00, 0x00, 0x00,
2740 // tag kRSEQ
2741 'R', 'S', 'E', 'Q',
2742 // end offset 16
2743 0x10, 0x00, 0x00, 0x00,
2744 // nonce proof
2745 0x89, 0x67, 0x45, 0x23,
2746 0x01, 0xEF, 0xCD, 0xAB,
2747 // rejected sequence number
2748 0xBC, 0x9A, 0x78, 0x56,
2749 0x34, 0x12, 0x00, 0x00,
2750 // trailing junk
2751 'j', 'u', 'n', 'k',
2754 string expected_error = "Unable to read reset message.";
2755 CheckProcessingFails(packet, arraysize(packet), expected_error,
2756 QUIC_INVALID_PUBLIC_RST_PACKET);
2759 TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
2760 unsigned char packet[] = {
2761 // public flags (public reset, 8 byte connection_id)
2762 0x0E,
2763 // connection_id
2764 0x10, 0x32, 0x54, 0x76,
2765 0x98, 0xBA, 0xDC, 0xFE,
2766 // message tag (kPRST)
2767 'P', 'R', 'S', 'T',
2768 // num_entries (3) + padding
2769 0x03, 0x00, 0x00, 0x00,
2770 // tag kRNON
2771 'R', 'N', 'O', 'N',
2772 // end offset 8
2773 0x08, 0x00, 0x00, 0x00,
2774 // tag kRSEQ
2775 'R', 'S', 'E', 'Q',
2776 // end offset 16
2777 0x10, 0x00, 0x00, 0x00,
2778 // tag kCADR
2779 'C', 'A', 'D', 'R',
2780 // end offset 24
2781 0x18, 0x00, 0x00, 0x00,
2782 // nonce proof
2783 0x89, 0x67, 0x45, 0x23,
2784 0x01, 0xEF, 0xCD, 0xAB,
2785 // rejected sequence number
2786 0xBC, 0x9A, 0x78, 0x56,
2787 0x34, 0x12, 0x00, 0x00,
2788 // client address: 4.31.198.44:443
2789 0x02, 0x00,
2790 0x04, 0x1F, 0xC6, 0x2C,
2791 0xBB, 0x01,
2794 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2795 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2796 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2797 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2798 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2799 visitor_.public_reset_packet_->public_header.connection_id);
2800 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2801 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2802 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2803 visitor_.public_reset_packet_->nonce_proof);
2804 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2805 visitor_.public_reset_packet_->rejected_sequence_number);
2806 EXPECT_EQ("4.31.198.44",
2807 IPAddressToString(visitor_.public_reset_packet_->
2808 client_address.address()));
2809 EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
2811 // Now test framing boundaries.
2812 for (size_t i = 0; i < arraysize(packet); ++i) {
2813 string expected_error;
2814 DVLOG(1) << "iteration: " << i;
2815 if (i < kConnectionIdOffset) {
2816 expected_error = "Unable to read public flags.";
2817 CheckProcessingFails(packet, i, expected_error,
2818 QUIC_INVALID_PACKET_HEADER);
2819 } else if (i < kPublicResetPacketMessageTagOffset) {
2820 expected_error = "Unable to read ConnectionId.";
2821 CheckProcessingFails(packet, i, expected_error,
2822 QUIC_INVALID_PACKET_HEADER);
2823 } else {
2824 expected_error = "Unable to read reset message.";
2825 CheckProcessingFails(packet, i, expected_error,
2826 QUIC_INVALID_PUBLIC_RST_PACKET);
2831 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
2832 unsigned char packet[] = {
2833 // public flags (version, 8 byte connection_id)
2834 0x3D,
2835 // connection_id
2836 0x10, 0x32, 0x54, 0x76,
2837 0x98, 0xBA, 0xDC, 0xFE,
2838 // version tag
2839 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2840 'Q', '2', '.', '0',
2843 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
2845 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2846 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2847 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2848 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
2849 EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
2850 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
2852 for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID; ++i) {
2853 string expected_error;
2854 QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
2855 if (i < kConnectionIdOffset) {
2856 expected_error = "Unable to read public flags.";
2857 } else if (i < kVersionOffset) {
2858 expected_error = "Unable to read ConnectionId.";
2859 } else {
2860 expected_error = "Unable to read supported version in negotiation.";
2861 error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
2863 CheckProcessingFails(packet, i, expected_error, error_code);
2867 TEST_P(QuicFramerTest, FecPacket) {
2868 unsigned char packet[] = {
2869 // public flags (8 byte connection_id)
2870 0x3C,
2871 // connection_id
2872 0x10, 0x32, 0x54, 0x76,
2873 0x98, 0xBA, 0xDC, 0xFE,
2874 // packet sequence number
2875 0xBC, 0x9A, 0x78, 0x56,
2876 0x34, 0x12,
2877 // private flags (fec group & FEC)
2878 0x06,
2879 // first fec protected packet offset
2880 0x01,
2882 // redundancy
2883 'a', 'b', 'c', 'd',
2884 'e', 'f', 'g', 'h',
2885 'i', 'j', 'k', 'l',
2886 'm', 'n', 'o', 'p',
2889 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2890 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2892 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2893 ASSERT_TRUE(visitor_.header_.get());
2894 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2896 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2897 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2898 ASSERT_EQ(1, visitor_.fec_count_);
2899 const QuicFecData& fec_data = *visitor_.fec_data_[0];
2900 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
2901 EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
2904 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
2905 QuicPacketHeader header;
2906 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2907 header.public_header.reset_flag = false;
2908 header.public_header.version_flag = false;
2909 header.fec_flag = false;
2910 header.entropy_flag = false;
2911 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2912 header.fec_group = 0;
2914 QuicPaddingFrame padding_frame;
2916 QuicFrames frames;
2917 frames.push_back(QuicFrame(&padding_frame));
2919 unsigned char packet[kMaxPacketSize] = {
2920 // public flags (8 byte connection_id)
2921 0x3C,
2922 // connection_id
2923 0x10, 0x32, 0x54, 0x76,
2924 0x98, 0xBA, 0xDC, 0xFE,
2925 // packet sequence number
2926 0xBC, 0x9A, 0x78, 0x56,
2927 0x34, 0x12,
2928 // private flags
2929 0x00,
2931 // frame type (padding frame)
2932 0x00,
2933 0x00, 0x00, 0x00, 0x00
2936 uint64 header_size =
2937 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2938 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2939 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2941 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2942 ASSERT_TRUE(data != nullptr);
2944 test::CompareCharArraysWithHexError("constructed packet",
2945 data->data(), data->length(),
2946 AsChars(packet),
2947 arraysize(packet));
2950 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
2951 QuicPacketHeader header;
2952 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2953 header.public_header.reset_flag = false;
2954 header.public_header.version_flag = false;
2955 header.fec_flag = false;
2956 header.entropy_flag = false;
2957 header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
2958 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2959 header.fec_group = 0;
2961 QuicPaddingFrame padding_frame;
2963 QuicFrames frames;
2964 frames.push_back(QuicFrame(&padding_frame));
2966 unsigned char packet[kMaxPacketSize] = {
2967 // public flags (8 byte connection_id and 4 byte sequence number)
2968 0x2C,
2969 // connection_id
2970 0x10, 0x32, 0x54, 0x76,
2971 0x98, 0xBA, 0xDC, 0xFE,
2972 // packet sequence number
2973 0xBC, 0x9A, 0x78, 0x56,
2974 // private flags
2975 0x00,
2977 // frame type (padding frame)
2978 0x00,
2979 0x00, 0x00, 0x00, 0x00
2982 uint64 header_size =
2983 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2984 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2985 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2987 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2988 ASSERT_TRUE(data != nullptr);
2990 test::CompareCharArraysWithHexError("constructed packet",
2991 data->data(), data->length(),
2992 AsChars(packet),
2993 arraysize(packet));
2996 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
2997 QuicPacketHeader header;
2998 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2999 header.public_header.reset_flag = false;
3000 header.public_header.version_flag = false;
3001 header.fec_flag = false;
3002 header.entropy_flag = false;
3003 header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
3004 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3005 header.fec_group = 0;
3007 QuicPaddingFrame padding_frame;
3009 QuicFrames frames;
3010 frames.push_back(QuicFrame(&padding_frame));
3012 unsigned char packet[kMaxPacketSize] = {
3013 // public flags (8 byte connection_id and 2 byte sequence number)
3014 0x1C,
3015 // connection_id
3016 0x10, 0x32, 0x54, 0x76,
3017 0x98, 0xBA, 0xDC, 0xFE,
3018 // packet sequence number
3019 0xBC, 0x9A,
3020 // private flags
3021 0x00,
3023 // frame type (padding frame)
3024 0x00,
3025 0x00, 0x00, 0x00, 0x00
3028 uint64 header_size =
3029 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3030 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3031 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3033 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3034 ASSERT_TRUE(data != nullptr);
3036 test::CompareCharArraysWithHexError("constructed packet",
3037 data->data(), data->length(),
3038 AsChars(packet),
3039 arraysize(packet));
3042 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
3043 QuicPacketHeader header;
3044 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3045 header.public_header.reset_flag = false;
3046 header.public_header.version_flag = false;
3047 header.fec_flag = false;
3048 header.entropy_flag = false;
3049 header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
3050 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3051 header.fec_group = 0;
3053 QuicPaddingFrame padding_frame;
3055 QuicFrames frames;
3056 frames.push_back(QuicFrame(&padding_frame));
3058 unsigned char packet[kMaxPacketSize] = {
3059 // public flags (8 byte connection_id and 1 byte sequence number)
3060 0x0C,
3061 // connection_id
3062 0x10, 0x32, 0x54, 0x76,
3063 0x98, 0xBA, 0xDC, 0xFE,
3064 // packet sequence number
3065 0xBC,
3066 // private flags
3067 0x00,
3069 // frame type (padding frame)
3070 0x00,
3071 0x00, 0x00, 0x00, 0x00
3074 uint64 header_size =
3075 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3076 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3077 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3079 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3080 ASSERT_TRUE(data != nullptr);
3082 test::CompareCharArraysWithHexError("constructed packet",
3083 data->data(), data->length(),
3084 AsChars(packet),
3085 arraysize(packet));
3088 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
3089 QuicPacketHeader header;
3090 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3091 header.public_header.reset_flag = false;
3092 header.public_header.version_flag = false;
3093 header.fec_flag = false;
3094 header.entropy_flag = true;
3095 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
3096 header.fec_group = 0;
3098 QuicStreamFrame stream_frame;
3099 stream_frame.stream_id = 0x01020304;
3100 stream_frame.fin = true;
3101 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
3102 stream_frame.data = MakeIOVector("hello world!");
3104 QuicFrames frames;
3105 frames.push_back(QuicFrame(&stream_frame));
3107 unsigned char packet[] = {
3108 // public flags (8 byte connection_id)
3109 0x3C,
3110 // connection_id
3111 0x10, 0x32, 0x54, 0x76,
3112 0x98, 0xBA, 0xDC, 0xFE,
3113 // packet sequence number
3114 0xBC, 0x9A, 0x78, 0x56,
3115 0x34, 0x12,
3116 // private flags (entropy)
3117 0x01,
3119 // frame type (stream frame with fin and no length)
3120 0xDF,
3121 // stream id
3122 0x04, 0x03, 0x02, 0x01,
3123 // offset
3124 0x54, 0x76, 0x10, 0x32,
3125 0xDC, 0xFE, 0x98, 0xBA,
3126 // data
3127 'h', 'e', 'l', 'l',
3128 'o', ' ', 'w', 'o',
3129 'r', 'l', 'd', '!',
3132 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3133 ASSERT_TRUE(data != nullptr);
3135 test::CompareCharArraysWithHexError("constructed packet",
3136 data->data(), data->length(),
3137 AsChars(packet), arraysize(packet));
3140 TEST_P(QuicFramerTest, BuildStreamFramePacketInFecGroup) {
3141 QuicPacketHeader header;
3142 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3143 header.public_header.reset_flag = false;
3144 header.public_header.version_flag = false;
3145 header.fec_flag = false;
3146 header.entropy_flag = true;
3147 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
3148 header.is_in_fec_group = IN_FEC_GROUP;
3149 header.fec_group = GG_UINT64_C(0x77123456789ABC);
3151 QuicStreamFrame stream_frame;
3152 stream_frame.stream_id = 0x01020304;
3153 stream_frame.fin = true;
3154 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
3155 stream_frame.data = MakeIOVector("hello world!");
3157 QuicFrames frames;
3158 frames.push_back(QuicFrame(&stream_frame));
3159 unsigned char packet[] = {
3160 // public flags (8 byte connection_id)
3161 0x3C,
3162 // connection_id
3163 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3164 // packet sequence number
3165 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3166 // private flags (entropy, is_in_fec_group)
3167 0x03,
3168 // FEC group
3169 0x00,
3170 // frame type (stream frame with fin and data length field)
3171 0xFF,
3172 // stream id
3173 0x04, 0x03, 0x02, 0x01,
3174 // offset
3175 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
3176 // data length (since packet is in an FEC group)
3177 0x0C, 0x00,
3178 // data
3179 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
3182 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3183 ASSERT_TRUE(data != nullptr);
3185 test::CompareCharArraysWithHexError("constructed packet",
3186 data->data(), data->length(),
3187 AsChars(packet), arraysize(packet));
3190 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
3191 QuicPacketHeader header;
3192 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3193 header.public_header.reset_flag = false;
3194 header.public_header.version_flag = true;
3195 header.fec_flag = false;
3196 header.entropy_flag = true;
3197 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
3198 header.fec_group = 0;
3200 QuicStreamFrame stream_frame;
3201 stream_frame.stream_id = 0x01020304;
3202 stream_frame.fin = true;
3203 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
3204 stream_frame.data = MakeIOVector("hello world!");
3206 QuicFrames frames;
3207 frames.push_back(QuicFrame(&stream_frame));
3209 unsigned char packet[] = {
3210 // public flags (version, 8 byte connection_id)
3211 0x3D,
3212 // connection_id
3213 0x10,
3214 0x32,
3215 0x54,
3216 0x76,
3217 0x98,
3218 0xBA,
3219 0xDC,
3220 0xFE,
3221 // version tag
3222 'Q',
3223 '0',
3224 GetQuicVersionDigitTens(),
3225 GetQuicVersionDigitOnes(),
3226 // packet sequence number
3227 0xBC,
3228 0x9A,
3229 0x78,
3230 0x56,
3231 0x34,
3232 0x12,
3233 // private flags (entropy)
3234 0x01,
3236 // frame type (stream frame with fin and no length)
3237 0xDF,
3238 // stream id
3239 0x04,
3240 0x03,
3241 0x02,
3242 0x01,
3243 // offset
3244 0x54,
3245 0x76,
3246 0x10,
3247 0x32,
3248 0xDC,
3249 0xFE,
3250 0x98,
3251 0xBA,
3252 // data
3253 'h',
3254 'e',
3255 'l',
3256 'l',
3257 'o',
3258 ' ',
3259 'w',
3260 'o',
3261 'r',
3262 'l',
3263 'd',
3264 '!',
3267 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
3268 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3269 ASSERT_TRUE(data != nullptr);
3271 test::CompareCharArraysWithHexError("constructed packet",
3272 data->data(), data->length(),
3273 AsChars(packet), arraysize(packet));
3276 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
3277 QuicPacketPublicHeader header;
3278 header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3279 header.reset_flag = false;
3280 header.version_flag = true;
3282 unsigned char packet[] = {
3283 // public flags (version, 8 byte connection_id)
3284 0x0D,
3285 // connection_id
3286 0x10,
3287 0x32,
3288 0x54,
3289 0x76,
3290 0x98,
3291 0xBA,
3292 0xDC,
3293 0xFE,
3294 // version tag
3295 'Q',
3296 '0',
3297 GetQuicVersionDigitTens(),
3298 GetQuicVersionDigitOnes(),
3301 QuicVersionVector versions;
3302 versions.push_back(GetParam());
3303 scoped_ptr<QuicEncryptedPacket> data(
3304 framer_.BuildVersionNegotiationPacket(header, versions));
3306 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3307 data->length(), AsChars(packet),
3308 arraysize(packet));
3311 TEST_P(QuicFramerTest, BuildAckFramePacket) {
3312 QuicPacketHeader header;
3313 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3314 header.public_header.reset_flag = false;
3315 header.public_header.version_flag = false;
3316 header.fec_flag = false;
3317 header.entropy_flag = true;
3318 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3319 header.fec_group = 0;
3321 QuicAckFrame ack_frame;
3322 ack_frame.entropy_hash = 0x43;
3323 ack_frame.largest_observed = GG_UINT64_C(0x770123456789ABF);
3324 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3325 ack_frame.missing_packets.insert(GG_UINT64_C(0x770123456789ABE));
3327 QuicFrames frames;
3328 frames.push_back(QuicFrame(&ack_frame));
3330 unsigned char packet[] = {
3331 // public flags (8 byte connection_id)
3332 0x3C,
3333 // connection_id
3334 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3335 // packet sequence number
3336 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3337 // private flags (entropy)
3338 0x01,
3340 // frame type (ack frame)
3341 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3342 0x6C,
3343 // entropy hash of all received packets.
3344 0x43,
3345 // largest observed packet sequence number
3346 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
3347 // Zero delta time.
3348 0x00, 0x00,
3349 // num received packets.
3350 0x00,
3351 // num missing packet ranges
3352 0x01,
3353 // missing packet delta
3354 0x01,
3355 // 0 more missing packets in range.
3356 0x00,
3357 // 0 revived packets.
3358 0x00,
3361 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3362 ASSERT_TRUE(data != nullptr);
3364 test::CompareCharArraysWithHexError("constructed packet",
3365 data->data(), data->length(),
3366 AsChars(packet), arraysize(packet));
3369 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3370 // revived packets. (In both the large and small packet cases below).
3372 TEST_P(QuicFramerTest, BuildTruncatedAckFrameLargePacket) {
3373 QuicPacketHeader header;
3374 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3375 header.public_header.reset_flag = false;
3376 header.public_header.version_flag = false;
3377 header.fec_flag = false;
3378 header.entropy_flag = true;
3379 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3380 header.fec_group = 0;
3382 QuicAckFrame ack_frame;
3383 // This entropy hash is different from what shows up in the packet below,
3384 // since entropy is recomputed by the framer on ack truncation (by
3385 // TestEntropyCalculator for this test.)
3386 ack_frame.entropy_hash = 0x43;
3387 ack_frame.largest_observed = 2 * 300;
3388 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3389 for (size_t i = 1; i < 2 * 300; i += 2) {
3390 ack_frame.missing_packets.insert(i);
3393 QuicFrames frames;
3394 frames.push_back(QuicFrame(&ack_frame));
3396 unsigned char packet[] = {
3397 // public flags (8 byte connection_id)
3398 0x3C,
3399 // connection_id
3400 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3401 // packet sequence number
3402 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3403 // private flags (entropy)
3404 0x01,
3406 // frame type (ack frame)
3407 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3408 0x74,
3409 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3410 // since ack is truncated.
3411 0x01,
3412 // 2-byte largest observed packet sequence number.
3413 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3414 0xFE, 0x01,
3415 // Zero delta time.
3416 0x00, 0x00,
3417 // num missing packet ranges (limited to 255 by size of this field).
3418 0xFF,
3419 // {missing packet delta, further missing packets in range}
3420 // 6 nack ranges x 42 + 3 nack ranges
3421 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3422 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3423 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3424 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3425 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3426 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3427 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3428 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3429 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3430 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3432 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3433 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3434 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3435 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3436 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3437 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3438 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3439 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3440 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3441 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3443 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3444 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3445 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3446 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3447 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3448 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3449 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3450 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3451 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3452 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3454 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3455 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3456 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3457 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3458 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3459 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3460 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3461 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3462 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3463 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3465 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3466 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3467 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3469 // 0 revived packets.
3470 0x00,
3473 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3474 ASSERT_TRUE(data != nullptr);
3476 test::CompareCharArraysWithHexError("constructed packet",
3477 data->data(), data->length(),
3478 AsChars(packet), arraysize(packet));
3481 TEST_P(QuicFramerTest, BuildTruncatedAckFrameSmallPacket) {
3482 QuicPacketHeader header;
3483 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3484 header.public_header.reset_flag = false;
3485 header.public_header.version_flag = false;
3486 header.fec_flag = false;
3487 header.entropy_flag = true;
3488 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3489 header.fec_group = 0;
3491 QuicAckFrame ack_frame;
3492 // This entropy hash is different from what shows up in the packet below,
3493 // since entropy is recomputed by the framer on ack truncation (by
3494 // TestEntropyCalculator for this test.)
3495 ack_frame.entropy_hash = 0x43;
3496 ack_frame.largest_observed = 2 * 300;
3497 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3498 for (size_t i = 1; i < 2 * 300; i += 2) {
3499 ack_frame.missing_packets.insert(i);
3502 QuicFrames frames;
3503 frames.push_back(QuicFrame(&ack_frame));
3505 unsigned char packet[] = {
3506 // public flags (8 byte connection_id)
3507 0x3C,
3508 // connection_id
3509 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3510 // packet sequence number
3511 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3512 // private flags (entropy)
3513 0x01,
3515 // frame type (ack frame)
3516 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3517 0x74,
3518 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3519 // since ack is truncated.
3520 0x01,
3521 // 2-byte largest observed packet sequence number.
3522 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3523 0x0C, 0x00,
3524 // Zero delta time.
3525 0x00, 0x00,
3526 // num missing packet ranges (limited to 6 by packet size of 37).
3527 0x06,
3528 // {missing packet delta, further missing packets in range}
3529 // 6 nack ranges
3530 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3531 // 0 revived packets.
3532 0x00,
3535 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames, 37u));
3536 ASSERT_TRUE(data != nullptr);
3537 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3538 EXPECT_EQ(36u, data->length());
3539 test::CompareCharArraysWithHexError("constructed packet",
3540 data->data(), data->length(),
3541 AsChars(packet), arraysize(packet));
3544 TEST_P(QuicFramerTest, BuildStopWaitingPacket) {
3545 QuicPacketHeader header;
3546 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3547 header.public_header.reset_flag = false;
3548 header.public_header.version_flag = false;
3549 header.fec_flag = false;
3550 header.entropy_flag = true;
3551 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3552 header.fec_group = 0;
3554 QuicStopWaitingFrame stop_waiting_frame;
3555 stop_waiting_frame.entropy_hash = 0x14;
3556 stop_waiting_frame.least_unacked = GG_UINT64_C(0x770123456789AA0);
3558 QuicFrames frames;
3559 frames.push_back(QuicFrame(&stop_waiting_frame));
3561 unsigned char packet[] = {
3562 // public flags (8 byte connection_id)
3563 0x3C,
3564 // connection_id
3565 0x10, 0x32, 0x54, 0x76,
3566 0x98, 0xBA, 0xDC, 0xFE,
3567 // packet sequence number
3568 0xA8, 0x9A, 0x78, 0x56,
3569 0x34, 0x12,
3570 // private flags (entropy)
3571 0x01,
3573 // frame type (stop waiting frame)
3574 0x06,
3575 // entropy hash of sent packets till least awaiting - 1.
3576 0x14,
3577 // least packet sequence number awaiting an ack, delta from sequence number.
3578 0x08, 0x00, 0x00, 0x00,
3579 0x00, 0x00,
3582 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3583 ASSERT_TRUE(data != nullptr);
3585 test::CompareCharArraysWithHexError("constructed packet",
3586 data->data(), data->length(),
3587 AsChars(packet), arraysize(packet));
3590 TEST_P(QuicFramerTest, BuildRstFramePacketQuicVersion24) {
3591 if (version_ > QUIC_VERSION_24) {
3592 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3593 return;
3596 QuicPacketHeader header;
3597 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3598 header.public_header.reset_flag = false;
3599 header.public_header.version_flag = false;
3600 header.fec_flag = false;
3601 header.entropy_flag = false;
3602 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3603 header.fec_group = 0;
3605 QuicRstStreamFrame rst_frame;
3606 rst_frame.stream_id = 0x01020304;
3607 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3608 rst_frame.error_details = "because I can";
3609 rst_frame.byte_offset = 0x0807060504030201;
3611 unsigned char packet[] = {
3612 // public flags (8 byte connection_id)
3613 0x3C,
3614 // connection_id
3615 0x10, 0x32, 0x54, 0x76,
3616 0x98, 0xBA, 0xDC, 0xFE,
3617 // packet sequence number
3618 0xBC, 0x9A, 0x78, 0x56,
3619 0x34, 0x12,
3620 // private flags
3621 0x00,
3623 // frame type (rst stream frame)
3624 0x01,
3625 // stream id
3626 0x04, 0x03, 0x02, 0x01,
3627 // sent byte offset
3628 0x01, 0x02, 0x03, 0x04,
3629 0x05, 0x06, 0x07, 0x08,
3630 // error code
3631 0x08, 0x07, 0x06, 0x05,
3632 // error details length
3633 0x0d, 0x00,
3634 // error details
3635 'b', 'e', 'c', 'a',
3636 'u', 's', 'e', ' ',
3637 'I', ' ', 'c', 'a',
3638 'n',
3641 QuicFrames frames;
3642 frames.push_back(QuicFrame(&rst_frame));
3644 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3645 ASSERT_TRUE(data != nullptr);
3647 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3648 data->length(), AsChars(packet),
3649 arraysize(packet));
3652 TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
3653 if (version_ <= QUIC_VERSION_24) {
3654 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3655 return;
3658 QuicPacketHeader header;
3659 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3660 header.public_header.reset_flag = false;
3661 header.public_header.version_flag = false;
3662 header.fec_flag = false;
3663 header.entropy_flag = false;
3664 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3665 header.fec_group = 0;
3667 QuicRstStreamFrame rst_frame;
3668 rst_frame.stream_id = 0x01020304;
3669 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3670 rst_frame.byte_offset = 0x0807060504030201;
3672 // clang-format off
3673 unsigned char packet[] = {
3674 // public flags (8 byte connection_id)
3675 0x3C,
3676 // connection_id
3677 0x10, 0x32, 0x54, 0x76,
3678 0x98, 0xBA, 0xDC, 0xFE,
3679 // packet sequence number
3680 0xBC, 0x9A, 0x78, 0x56,
3681 0x34, 0x12,
3682 // private flags
3683 0x00,
3685 // frame type (rst stream frame)
3686 0x01,
3687 // stream id
3688 0x04, 0x03, 0x02, 0x01,
3689 // sent byte offset
3690 0x01, 0x02, 0x03, 0x04,
3691 0x05, 0x06, 0x07, 0x08,
3692 // error code
3693 0x08, 0x07, 0x06, 0x05,
3695 // clang-format on
3697 QuicFrames frames;
3698 frames.push_back(QuicFrame(&rst_frame));
3700 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3701 ASSERT_TRUE(data != nullptr);
3703 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3704 data->length(), AsChars(packet),
3705 arraysize(packet));
3708 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
3709 QuicPacketHeader header;
3710 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3711 header.public_header.reset_flag = false;
3712 header.public_header.version_flag = false;
3713 header.fec_flag = false;
3714 header.entropy_flag = true;
3715 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3716 header.fec_group = 0;
3718 QuicConnectionCloseFrame close_frame;
3719 close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3720 close_frame.error_details = "because I can";
3722 QuicFrames frames;
3723 frames.push_back(QuicFrame(&close_frame));
3725 unsigned char packet[] = {
3726 // public flags (8 byte connection_id)
3727 0x3C,
3728 // connection_id
3729 0x10, 0x32, 0x54, 0x76,
3730 0x98, 0xBA, 0xDC, 0xFE,
3731 // packet sequence number
3732 0xBC, 0x9A, 0x78, 0x56,
3733 0x34, 0x12,
3734 // private flags (entropy)
3735 0x01,
3737 // frame type (connection close frame)
3738 0x02,
3739 // error code
3740 0x08, 0x07, 0x06, 0x05,
3741 // error details length
3742 0x0d, 0x00,
3743 // error details
3744 'b', 'e', 'c', 'a',
3745 'u', 's', 'e', ' ',
3746 'I', ' ', 'c', 'a',
3747 'n',
3750 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3751 ASSERT_TRUE(data != nullptr);
3753 test::CompareCharArraysWithHexError("constructed packet",
3754 data->data(), data->length(),
3755 AsChars(packet), arraysize(packet));
3758 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
3759 QuicPacketHeader header;
3760 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3761 header.public_header.reset_flag = false;
3762 header.public_header.version_flag = false;
3763 header.fec_flag = false;
3764 header.entropy_flag = true;
3765 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3766 header.fec_group = 0;
3768 QuicGoAwayFrame goaway_frame;
3769 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3770 goaway_frame.last_good_stream_id = 0x01020304;
3771 goaway_frame.reason_phrase = "because I can";
3773 QuicFrames frames;
3774 frames.push_back(QuicFrame(&goaway_frame));
3776 unsigned char packet[] = {
3777 // public flags (8 byte connection_id)
3778 0x3C,
3779 // connection_id
3780 0x10, 0x32, 0x54, 0x76,
3781 0x98, 0xBA, 0xDC, 0xFE,
3782 // packet sequence number
3783 0xBC, 0x9A, 0x78, 0x56,
3784 0x34, 0x12,
3785 // private flags(entropy)
3786 0x01,
3788 // frame type (go away frame)
3789 0x03,
3790 // error code
3791 0x08, 0x07, 0x06, 0x05,
3792 // stream id
3793 0x04, 0x03, 0x02, 0x01,
3794 // error details length
3795 0x0d, 0x00,
3796 // error details
3797 'b', 'e', 'c', 'a',
3798 'u', 's', 'e', ' ',
3799 'I', ' ', 'c', 'a',
3800 'n',
3803 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3804 ASSERT_TRUE(data != nullptr);
3806 test::CompareCharArraysWithHexError("constructed packet",
3807 data->data(), data->length(),
3808 AsChars(packet), arraysize(packet));
3811 TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
3812 QuicPacketHeader header;
3813 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3814 header.public_header.reset_flag = false;
3815 header.public_header.version_flag = false;
3816 header.fec_flag = false;
3817 header.entropy_flag = true;
3818 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3819 header.fec_group = 0;
3821 QuicWindowUpdateFrame window_update_frame;
3822 window_update_frame.stream_id = 0x01020304;
3823 window_update_frame.byte_offset = 0x1122334455667788;
3825 QuicFrames frames;
3826 frames.push_back(QuicFrame(&window_update_frame));
3828 unsigned char packet[] = {
3829 // public flags (8 byte connection_id)
3830 0x3C,
3831 // connection_id
3832 0x10, 0x32, 0x54, 0x76,
3833 0x98, 0xBA, 0xDC, 0xFE,
3834 // packet sequence number
3835 0xBC, 0x9A, 0x78, 0x56,
3836 0x34, 0x12,
3837 // private flags(entropy)
3838 0x01,
3840 // frame type (window update frame)
3841 0x04,
3842 // stream id
3843 0x04, 0x03, 0x02, 0x01,
3844 // byte offset
3845 0x88, 0x77, 0x66, 0x55,
3846 0x44, 0x33, 0x22, 0x11,
3849 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3850 ASSERT_TRUE(data != nullptr);
3852 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3853 data->length(), AsChars(packet),
3854 arraysize(packet));
3857 TEST_P(QuicFramerTest, BuildBlockedPacket) {
3858 QuicPacketHeader header;
3859 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3860 header.public_header.reset_flag = false;
3861 header.public_header.version_flag = false;
3862 header.fec_flag = false;
3863 header.entropy_flag = true;
3864 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3865 header.fec_group = 0;
3867 QuicBlockedFrame blocked_frame;
3868 blocked_frame.stream_id = 0x01020304;
3870 QuicFrames frames;
3871 frames.push_back(QuicFrame(&blocked_frame));
3873 unsigned char packet[] = {
3874 // public flags (8 byte connection_id)
3875 0x3C,
3876 // connection_id
3877 0x10, 0x32, 0x54, 0x76,
3878 0x98, 0xBA, 0xDC, 0xFE,
3879 // packet sequence number
3880 0xBC, 0x9A, 0x78, 0x56,
3881 0x34, 0x12,
3882 // private flags(entropy)
3883 0x01,
3885 // frame type (blocked frame)
3886 0x05,
3887 // stream id
3888 0x04, 0x03, 0x02, 0x01,
3891 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3892 ASSERT_TRUE(data != nullptr);
3894 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3895 data->length(), AsChars(packet),
3896 arraysize(packet));
3899 TEST_P(QuicFramerTest, BuildPingPacket) {
3900 QuicPacketHeader header;
3901 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3902 header.public_header.reset_flag = false;
3903 header.public_header.version_flag = false;
3904 header.fec_flag = false;
3905 header.entropy_flag = true;
3906 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3907 header.fec_group = 0;
3909 QuicPingFrame ping_frame;
3911 QuicFrames frames;
3912 frames.push_back(QuicFrame(&ping_frame));
3914 unsigned char packet[] = {
3915 // public flags (8 byte connection_id)
3916 0x3C,
3917 // connection_id
3918 0x10, 0x32, 0x54, 0x76,
3919 0x98, 0xBA, 0xDC, 0xFE,
3920 // packet sequence number
3921 0xBC, 0x9A, 0x78, 0x56,
3922 0x34, 0x12,
3923 // private flags(entropy)
3924 0x01,
3926 // frame type (ping frame)
3927 0x07,
3930 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3931 ASSERT_TRUE(data != nullptr);
3933 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3934 data->length(), AsChars(packet),
3935 arraysize(packet));
3938 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
3939 QuicPublicResetPacket reset_packet;
3940 reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3941 reset_packet.public_header.reset_flag = true;
3942 reset_packet.public_header.version_flag = false;
3943 reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
3944 reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
3946 unsigned char packet[] = {
3947 // public flags (public reset, 8 byte ConnectionId)
3948 0x0E,
3949 // connection_id
3950 0x10, 0x32, 0x54, 0x76,
3951 0x98, 0xBA, 0xDC, 0xFE,
3952 // message tag (kPRST)
3953 'P', 'R', 'S', 'T',
3954 // num_entries (2) + padding
3955 0x02, 0x00, 0x00, 0x00,
3956 // tag kRNON
3957 'R', 'N', 'O', 'N',
3958 // end offset 8
3959 0x08, 0x00, 0x00, 0x00,
3960 // tag kRSEQ
3961 'R', 'S', 'E', 'Q',
3962 // end offset 16
3963 0x10, 0x00, 0x00, 0x00,
3964 // nonce proof
3965 0x89, 0x67, 0x45, 0x23,
3966 0x01, 0xEF, 0xCD, 0xAB,
3967 // rejected sequence number
3968 0xBC, 0x9A, 0x78, 0x56,
3969 0x34, 0x12, 0x00, 0x00,
3972 scoped_ptr<QuicEncryptedPacket> data(
3973 framer_.BuildPublicResetPacket(reset_packet));
3974 ASSERT_TRUE(data != nullptr);
3976 test::CompareCharArraysWithHexError("constructed packet",
3977 data->data(), data->length(),
3978 AsChars(packet), arraysize(packet));
3981 TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
3982 QuicPublicResetPacket reset_packet;
3983 reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3984 reset_packet.public_header.reset_flag = true;
3985 reset_packet.public_header.version_flag = false;
3986 reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
3987 reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
3988 reset_packet.client_address = IPEndPoint(Loopback4(), 0x1234);
3990 unsigned char packet[] = {
3991 // public flags (public reset, 8 byte ConnectionId)
3992 0x0E,
3993 // connection_id
3994 0x10, 0x32, 0x54, 0x76,
3995 0x98, 0xBA, 0xDC, 0xFE,
3996 // message tag (kPRST)
3997 'P', 'R', 'S', 'T',
3998 // num_entries (3) + padding
3999 0x03, 0x00, 0x00, 0x00,
4000 // tag kRNON
4001 'R', 'N', 'O', 'N',
4002 // end offset 8
4003 0x08, 0x00, 0x00, 0x00,
4004 // tag kRSEQ
4005 'R', 'S', 'E', 'Q',
4006 // end offset 16
4007 0x10, 0x00, 0x00, 0x00,
4008 // tag kCADR
4009 'C', 'A', 'D', 'R',
4010 // end offset 24
4011 0x18, 0x00, 0x00, 0x00,
4012 // nonce proof
4013 0x89, 0x67, 0x45, 0x23,
4014 0x01, 0xEF, 0xCD, 0xAB,
4015 // rejected sequence number
4016 0xBC, 0x9A, 0x78, 0x56,
4017 0x34, 0x12, 0x00, 0x00,
4018 // client address
4019 0x02, 0x00,
4020 0x7F, 0x00, 0x00, 0x01,
4021 0x34, 0x12,
4024 scoped_ptr<QuicEncryptedPacket> data(
4025 framer_.BuildPublicResetPacket(reset_packet));
4026 ASSERT_TRUE(data != nullptr);
4028 test::CompareCharArraysWithHexError("constructed packet",
4029 data->data(), data->length(),
4030 AsChars(packet), arraysize(packet));
4033 TEST_P(QuicFramerTest, BuildFecPacket) {
4034 QuicPacketHeader header;
4035 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4036 header.public_header.reset_flag = false;
4037 header.public_header.version_flag = false;
4038 header.fec_flag = true;
4039 header.entropy_flag = true;
4040 header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
4041 header.is_in_fec_group = IN_FEC_GROUP;
4042 header.fec_group = GG_UINT64_C(0x123456789ABB);;
4044 QuicFecData fec_data;
4045 fec_data.fec_group = 1;
4046 fec_data.redundancy = "abcdefghijklmnop";
4048 unsigned char packet[] = {
4049 // public flags (8 byte connection_id)
4050 0x3C,
4051 // connection_id
4052 0x10, 0x32, 0x54, 0x76,
4053 0x98, 0xBA, 0xDC, 0xFE,
4054 // packet sequence number
4055 0xBC, 0x9A, 0x78, 0x56,
4056 0x34, 0x12,
4057 // private flags (entropy & fec group & fec packet)
4058 0x07,
4059 // first fec protected packet offset
4060 0x01,
4062 // redundancy
4063 'a', 'b', 'c', 'd',
4064 'e', 'f', 'g', 'h',
4065 'i', 'j', 'k', 'l',
4066 'm', 'n', 'o', 'p',
4069 scoped_ptr<QuicPacket> data(framer_.BuildFecPacket(header, fec_data));
4070 ASSERT_TRUE(data != nullptr);
4072 test::CompareCharArraysWithHexError("constructed packet",
4073 data->data(), data->length(),
4074 AsChars(packet), arraysize(packet));
4077 TEST_P(QuicFramerTest, EncryptPacket) {
4078 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
4079 unsigned char packet[] = {
4080 // public flags (8 byte connection_id)
4081 0x3C,
4082 // connection_id
4083 0x10, 0x32, 0x54, 0x76,
4084 0x98, 0xBA, 0xDC, 0xFE,
4085 // packet sequence number
4086 0xBC, 0x9A, 0x78, 0x56,
4087 0x34, 0x12,
4088 // private flags (fec group & fec packet)
4089 0x06,
4090 // first fec protected packet offset
4091 0x01,
4093 // redundancy
4094 'a', 'b', 'c', 'd',
4095 'e', 'f', 'g', 'h',
4096 'i', 'j', 'k', 'l',
4097 'm', 'n', 'o', 'p',
4100 scoped_ptr<QuicPacket> raw(new QuicPacket(
4101 AsChars(packet), arraysize(packet), false, PACKET_8BYTE_CONNECTION_ID,
4102 !kIncludeVersion, PACKET_6BYTE_SEQUENCE_NUMBER));
4103 char buffer[kMaxPacketSize];
4104 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
4105 ENCRYPTION_NONE, sequence_number, *raw, buffer, kMaxPacketSize));
4107 ASSERT_TRUE(encrypted.get() != nullptr);
4108 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
4111 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
4112 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
4113 unsigned char packet[] = {
4114 // public flags (version, 8 byte connection_id)
4115 0x3D,
4116 // connection_id
4117 0x10, 0x32, 0x54, 0x76,
4118 0x98, 0xBA, 0xDC, 0xFE,
4119 // version tag
4120 'Q', '.', '1', '0',
4121 // packet sequence number
4122 0xBC, 0x9A, 0x78, 0x56,
4123 0x34, 0x12,
4124 // private flags (fec group & fec flags)
4125 0x06,
4126 // first fec protected packet offset
4127 0x01,
4129 // redundancy
4130 'a', 'b', 'c', 'd',
4131 'e', 'f', 'g', 'h',
4132 'i', 'j', 'k', 'l',
4133 'm', 'n', 'o', 'p',
4136 scoped_ptr<QuicPacket> raw(new QuicPacket(
4137 AsChars(packet), arraysize(packet), false, PACKET_8BYTE_CONNECTION_ID,
4138 kIncludeVersion, PACKET_6BYTE_SEQUENCE_NUMBER));
4139 char buffer[kMaxPacketSize];
4140 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
4141 ENCRYPTION_NONE, sequence_number, *raw, buffer, kMaxPacketSize));
4143 ASSERT_TRUE(encrypted.get() != nullptr);
4144 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
4147 TEST_P(QuicFramerTest, AckTruncationLargePacket) {
4148 QuicPacketHeader header;
4149 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4150 header.public_header.reset_flag = false;
4151 header.public_header.version_flag = false;
4152 header.fec_flag = false;
4153 header.entropy_flag = false;
4154 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4155 header.fec_group = 0;
4157 // Create a packet with just the ack.
4158 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
4159 QuicFrame frame;
4160 frame.type = ACK_FRAME;
4161 frame.ack_frame = &ack_frame;
4162 QuicFrames frames;
4163 frames.push_back(frame);
4165 // Build an ack packet with truncation due to limit in number of nack ranges.
4166 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
4167 ASSERT_TRUE(raw_ack_packet != nullptr);
4168 char buffer[kMaxPacketSize];
4169 scoped_ptr<QuicEncryptedPacket> ack_packet(
4170 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
4171 *raw_ack_packet, buffer, kMaxPacketSize));
4172 // Now make sure we can turn our ack packet back into an ack frame.
4173 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
4174 ASSERT_EQ(1u, visitor_.ack_frames_.size());
4175 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
4176 EXPECT_TRUE(processed_ack_frame.is_truncated);
4177 EXPECT_EQ(510u, processed_ack_frame.largest_observed);
4178 ASSERT_EQ(255u, processed_ack_frame.missing_packets.size());
4179 SequenceNumberSet::const_iterator missing_iter =
4180 processed_ack_frame.missing_packets.begin();
4181 EXPECT_EQ(1u, *missing_iter);
4182 SequenceNumberSet::const_reverse_iterator last_missing_iter =
4183 processed_ack_frame.missing_packets.rbegin();
4184 EXPECT_EQ(509u, *last_missing_iter);
4187 TEST_P(QuicFramerTest, AckTruncationSmallPacket) {
4188 QuicPacketHeader header;
4189 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4190 header.public_header.reset_flag = false;
4191 header.public_header.version_flag = false;
4192 header.fec_flag = false;
4193 header.entropy_flag = false;
4194 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4195 header.fec_group = 0;
4197 // Create a packet with just the ack.
4198 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
4199 QuicFrame frame;
4200 frame.type = ACK_FRAME;
4201 frame.ack_frame = &ack_frame;
4202 QuicFrames frames;
4203 frames.push_back(frame);
4205 // Build an ack packet with truncation due to limit in number of nack ranges.
4206 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames, 500));
4207 ASSERT_TRUE(raw_ack_packet != nullptr);
4208 char buffer[kMaxPacketSize];
4209 scoped_ptr<QuicEncryptedPacket> ack_packet(
4210 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
4211 *raw_ack_packet, buffer, kMaxPacketSize));
4212 // Now make sure we can turn our ack packet back into an ack frame.
4213 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
4214 ASSERT_EQ(1u, visitor_.ack_frames_.size());
4215 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
4216 EXPECT_TRUE(processed_ack_frame.is_truncated);
4217 EXPECT_EQ(476u, processed_ack_frame.largest_observed);
4218 ASSERT_EQ(238u, processed_ack_frame.missing_packets.size());
4219 SequenceNumberSet::const_iterator missing_iter =
4220 processed_ack_frame.missing_packets.begin();
4221 EXPECT_EQ(1u, *missing_iter);
4222 SequenceNumberSet::const_reverse_iterator last_missing_iter =
4223 processed_ack_frame.missing_packets.rbegin();
4224 EXPECT_EQ(475u, *last_missing_iter);
4227 TEST_P(QuicFramerTest, CleanTruncation) {
4228 QuicPacketHeader header;
4229 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4230 header.public_header.reset_flag = false;
4231 header.public_header.version_flag = false;
4232 header.fec_flag = false;
4233 header.entropy_flag = true;
4234 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4235 header.fec_group = 0;
4237 QuicAckFrame ack_frame;
4238 ack_frame.largest_observed = 201;
4239 for (uint64 i = 1; i < ack_frame.largest_observed; ++i) {
4240 ack_frame.missing_packets.insert(i);
4243 // Create a packet with just the ack.
4244 QuicFrame frame;
4245 frame.type = ACK_FRAME;
4246 frame.ack_frame = &ack_frame;
4247 QuicFrames frames;
4248 frames.push_back(frame);
4250 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
4251 ASSERT_TRUE(raw_ack_packet != nullptr);
4253 char buffer[kMaxPacketSize];
4254 scoped_ptr<QuicEncryptedPacket> ack_packet(
4255 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
4256 *raw_ack_packet, buffer, kMaxPacketSize));
4258 // Now make sure we can turn our ack packet back into an ack frame.
4259 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
4261 // Test for clean truncation of the ack by comparing the length of the
4262 // original packets to the re-serialized packets.
4263 frames.clear();
4264 frame.type = ACK_FRAME;
4265 frame.ack_frame = visitor_.ack_frames_[0];
4266 frames.push_back(frame);
4268 size_t original_raw_length = raw_ack_packet->length();
4269 raw_ack_packet.reset(BuildDataPacket(header, frames));
4270 ASSERT_TRUE(raw_ack_packet != nullptr);
4271 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
4272 ASSERT_TRUE(raw_ack_packet != nullptr);
4275 TEST_P(QuicFramerTest, EntropyFlagTest) {
4276 unsigned char packet[] = {
4277 // public flags (8 byte connection_id)
4278 0x3C,
4279 // connection_id
4280 0x10, 0x32, 0x54, 0x76,
4281 0x98, 0xBA, 0xDC, 0xFE,
4282 // packet sequence number
4283 0xBC, 0x9A, 0x78, 0x56,
4284 0x34, 0x12,
4285 // private flags (Entropy)
4286 0x01,
4288 // frame type (stream frame with fin and no length)
4289 0xDF,
4290 // stream id
4291 0x04, 0x03, 0x02, 0x01,
4292 // offset
4293 0x54, 0x76, 0x10, 0x32,
4294 0xDC, 0xFE, 0x98, 0xBA,
4295 // data
4296 'h', 'e', 'l', 'l',
4297 'o', ' ', 'w', 'o',
4298 'r', 'l', 'd', '!',
4301 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4302 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4303 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4304 ASSERT_TRUE(visitor_.header_.get());
4305 EXPECT_TRUE(visitor_.header_->entropy_flag);
4306 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4307 EXPECT_FALSE(visitor_.header_->fec_flag);
4310 TEST_P(QuicFramerTest, FecEntropyTest) {
4311 unsigned char packet[] = {
4312 // public flags (8 byte connection_id)
4313 0x3C,
4314 // connection_id
4315 0x10, 0x32, 0x54, 0x76,
4316 0x98, 0xBA, 0xDC, 0xFE,
4317 // packet sequence number
4318 0xBC, 0x9A, 0x78, 0x56,
4319 0x34, 0x12,
4320 // private flags (Entropy & fec group & FEC)
4321 0x07,
4322 // first fec protected packet offset
4323 0xFF,
4325 // frame type (stream frame with fin and no length)
4326 0xDF,
4327 // stream id
4328 0x04, 0x03, 0x02, 0x01,
4329 // offset
4330 0x54, 0x76, 0x10, 0x32,
4331 0xDC, 0xFE, 0x98, 0xBA,
4332 // data
4333 'h', 'e', 'l', 'l',
4334 'o', ' ', 'w', 'o',
4335 'r', 'l', 'd', '!',
4338 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4339 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4340 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4341 ASSERT_TRUE(visitor_.header_.get());
4342 EXPECT_TRUE(visitor_.header_->fec_flag);
4343 EXPECT_TRUE(visitor_.header_->entropy_flag);
4344 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4347 TEST_P(QuicFramerTest, StopPacketProcessing) {
4348 unsigned char packet[] = {
4349 // public flags (8 byte connection_id)
4350 0x3C,
4351 // connection_id
4352 0x10, 0x32, 0x54, 0x76,
4353 0x98, 0xBA, 0xDC, 0xFE,
4354 // packet sequence number
4355 0xBC, 0x9A, 0x78, 0x56,
4356 0x34, 0x12,
4357 // Entropy
4358 0x01,
4360 // frame type (stream frame with fin)
4361 0xFF,
4362 // stream id
4363 0x04, 0x03, 0x02, 0x01,
4364 // offset
4365 0x54, 0x76, 0x10, 0x32,
4366 0xDC, 0xFE, 0x98, 0xBA,
4367 // data length
4368 0x0c, 0x00,
4369 // data
4370 'h', 'e', 'l', 'l',
4371 'o', ' ', 'w', 'o',
4372 'r', 'l', 'd', '!',
4374 // frame type (ack frame)
4375 0x40,
4376 // entropy hash of sent packets till least awaiting - 1.
4377 0x14,
4378 // least packet sequence number awaiting an ack
4379 0xA0, 0x9A, 0x78, 0x56,
4380 0x34, 0x12,
4381 // entropy hash of all received packets.
4382 0x43,
4383 // largest observed packet sequence number
4384 0xBF, 0x9A, 0x78, 0x56,
4385 0x34, 0x12,
4386 // num missing packets
4387 0x01,
4388 // missing packet
4389 0xBE, 0x9A, 0x78, 0x56,
4390 0x34, 0x12,
4393 MockFramerVisitor visitor;
4394 framer_.set_visitor(&visitor);
4395 EXPECT_CALL(visitor, OnPacket());
4396 EXPECT_CALL(visitor, OnPacketHeader(_));
4397 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
4398 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
4399 EXPECT_CALL(visitor, OnPacketComplete());
4400 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
4401 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)).WillOnce(Return(true));
4402 EXPECT_CALL(visitor, OnDecryptedPacket(_));
4404 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4405 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4406 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4409 } // namespace test
4410 } // namespace net