43c2984fdd4dca4e80d2ffde42fcbd3e76175a4f
[platform/framework/web/crosswalk.git] / src / net / quic / quic_framer_test.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/quic/quic_framer.h"
6
7 #include <algorithm>
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
24
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::_;
35
36 namespace net {
37 namespace test {
38
39 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
40 const QuicPacketSequenceNumber kMask = kEpoch - 1;
41
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;
46
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(QuicVersion version) {
50   return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
51 }
52
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);
58 }
59
60 size_t GetSequenceNumberOffset(bool include_version) {
61   return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
62 }
63
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;
69 }
70
71 size_t GetPrivateFlagsOffset(bool include_version) {
72   return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
73 }
74
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;
79 }
80
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;
86 }
87
88 size_t GetFecGroupOffset(bool include_version) {
89   return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
90       kPrivateFlagsSize;
91 }
92
93 size_t GetFecGroupOffset(bool include_version,
94                          QuicSequenceNumberLength sequence_number_length) {
95   return GetPrivateFlagsOffset(include_version, sequence_number_length) +
96       kPrivateFlagsSize;
97 }
98
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;
103
104 // TODO(wtc): remove this when we drop support for QUIC_VERSION_13.
105 // Index into the nonce proof of the public reset packet.
106 // Public resets always have full connection_ids.
107 const size_t kPublicResetPacketNonceProofOffset =
108     kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
109
110 // TODO(wtc): remove this when we drop support for QUIC_VERSION_13.
111 // Index into the rejected sequence number of the public reset packet.
112 const size_t kPublicResetPacketRejectedSequenceNumberOffset =
113     kPublicResetPacketNonceProofOffset + kPublicResetNonceSize;
114
115 class TestEncrypter : public QuicEncrypter {
116  public:
117   virtual ~TestEncrypter() {}
118   virtual bool SetKey(StringPiece key) OVERRIDE {
119     return true;
120   }
121   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
122     return true;
123   }
124   virtual bool Encrypt(StringPiece nonce,
125                        StringPiece associated_data,
126                        StringPiece plaintext,
127                        unsigned char* output) OVERRIDE {
128     CHECK(false) << "Not implemented";
129     return false;
130   }
131   virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number,
132                                   StringPiece associated_data,
133                                   StringPiece plaintext) OVERRIDE {
134     sequence_number_ = sequence_number;
135     associated_data_ = associated_data.as_string();
136     plaintext_ = plaintext.as_string();
137     return new QuicData(plaintext.data(), plaintext.length());
138   }
139   virtual size_t GetKeySize() const OVERRIDE {
140     return 0;
141   }
142   virtual size_t GetNoncePrefixSize() const OVERRIDE {
143     return 0;
144   }
145   virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
146     return ciphertext_size;
147   }
148   virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
149     return plaintext_size;
150   }
151   virtual StringPiece GetKey() const OVERRIDE {
152     return StringPiece();
153   }
154   virtual StringPiece GetNoncePrefix() const OVERRIDE {
155     return StringPiece();
156   }
157   QuicPacketSequenceNumber sequence_number_;
158   string associated_data_;
159   string plaintext_;
160 };
161
162 class TestDecrypter : public QuicDecrypter {
163  public:
164   virtual ~TestDecrypter() {}
165   virtual bool SetKey(StringPiece key) OVERRIDE {
166     return true;
167   }
168   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
169     return true;
170   }
171   virtual bool Decrypt(StringPiece nonce,
172                        StringPiece associated_data,
173                        StringPiece ciphertext,
174                        unsigned char* output,
175                        size_t* output_length) OVERRIDE {
176     CHECK(false) << "Not implemented";
177     return false;
178   }
179   virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number,
180                                   StringPiece associated_data,
181                                   StringPiece ciphertext) OVERRIDE {
182     sequence_number_ = sequence_number;
183     associated_data_ = associated_data.as_string();
184     ciphertext_ = ciphertext.as_string();
185     return new QuicData(ciphertext.data(), ciphertext.length());
186   }
187   virtual StringPiece GetKey() const OVERRIDE {
188     return StringPiece();
189   }
190   virtual StringPiece GetNoncePrefix() const OVERRIDE {
191     return StringPiece();
192   }
193   QuicPacketSequenceNumber sequence_number_;
194   string associated_data_;
195   string ciphertext_;
196 };
197
198 class TestQuicVisitor : public ::net::QuicFramerVisitorInterface {
199  public:
200   TestQuicVisitor()
201       : error_count_(0),
202         version_mismatch_(0),
203         packet_count_(0),
204         frame_count_(0),
205         fec_count_(0),
206         complete_packets_(0),
207         revived_packets_(0),
208         accept_packet_(true),
209         accept_public_header_(true) {
210   }
211
212   virtual ~TestQuicVisitor() {
213     STLDeleteElements(&stream_frames_);
214     STLDeleteElements(&ack_frames_);
215     STLDeleteElements(&congestion_feedback_frames_);
216     STLDeleteElements(&stop_waiting_frames_);
217     STLDeleteElements(&fec_data_);
218   }
219
220   virtual void OnError(QuicFramer* f) OVERRIDE {
221     DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
222              << " (" << f->error() << ")";
223     ++error_count_;
224   }
225
226   virtual void OnPacket() OVERRIDE {}
227
228   virtual void OnPublicResetPacket(
229       const QuicPublicResetPacket& packet) OVERRIDE {
230     public_reset_packet_.reset(new QuicPublicResetPacket(packet));
231   }
232
233   virtual void OnVersionNegotiationPacket(
234       const QuicVersionNegotiationPacket& packet) OVERRIDE {
235     version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
236   }
237
238   virtual void OnRevivedPacket() OVERRIDE {
239     ++revived_packets_;
240   }
241
242   virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
243     DVLOG(1) << "QuicFramer Version Mismatch, version: " << version;
244     ++version_mismatch_;
245     return true;
246   }
247
248   virtual bool OnUnauthenticatedPublicHeader(
249       const QuicPacketPublicHeader& header) OVERRIDE {
250     public_header_.reset(new QuicPacketPublicHeader(header));
251     return accept_public_header_;
252   }
253
254   virtual bool OnUnauthenticatedHeader(
255       const QuicPacketHeader& header) OVERRIDE {
256     return true;
257   }
258
259   virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE {
260     ++packet_count_;
261     header_.reset(new QuicPacketHeader(header));
262     return accept_packet_;
263   }
264
265   virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE {
266     ++frame_count_;
267     stream_frames_.push_back(new QuicStreamFrame(frame));
268     return true;
269   }
270
271   virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {
272     fec_protected_payload_ = payload.as_string();
273   }
274
275   virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE {
276     ++frame_count_;
277     ack_frames_.push_back(new QuicAckFrame(frame));
278     return true;
279   }
280
281   virtual bool OnCongestionFeedbackFrame(
282       const QuicCongestionFeedbackFrame& frame) OVERRIDE {
283     ++frame_count_;
284     congestion_feedback_frames_.push_back(
285         new QuicCongestionFeedbackFrame(frame));
286     return true;
287   }
288
289   virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) OVERRIDE {
290     ++frame_count_;
291     stop_waiting_frames_.push_back(new QuicStopWaitingFrame(frame));
292     return true;
293   }
294
295   virtual void OnFecData(const QuicFecData& fec) OVERRIDE {
296     ++fec_count_;
297     fec_data_.push_back(new QuicFecData(fec));
298   }
299
300   virtual void OnPacketComplete() OVERRIDE {
301     ++complete_packets_;
302   }
303
304   virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE {
305     rst_stream_frame_ = frame;
306     return true;
307   }
308
309   virtual bool OnConnectionCloseFrame(
310       const QuicConnectionCloseFrame& frame) OVERRIDE {
311     connection_close_frame_ = frame;
312     return true;
313   }
314
315   virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE {
316     goaway_frame_ = frame;
317     return true;
318   }
319
320   virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame)
321       OVERRIDE {
322     window_update_frame_ = frame;
323     return true;
324   }
325
326   virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE {
327     blocked_frame_ = frame;
328     return true;
329   }
330
331   // Counters from the visitor_ callbacks.
332   int error_count_;
333   int version_mismatch_;
334   int packet_count_;
335   int frame_count_;
336   int fec_count_;
337   int complete_packets_;
338   int revived_packets_;
339   bool accept_packet_;
340   bool accept_public_header_;
341
342   scoped_ptr<QuicPacketHeader> header_;
343   scoped_ptr<QuicPacketPublicHeader> public_header_;
344   scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
345   scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
346   vector<QuicStreamFrame*> stream_frames_;
347   vector<QuicAckFrame*> ack_frames_;
348   vector<QuicCongestionFeedbackFrame*> congestion_feedback_frames_;
349   vector<QuicStopWaitingFrame*> stop_waiting_frames_;
350   vector<QuicFecData*> fec_data_;
351   string fec_protected_payload_;
352   QuicRstStreamFrame rst_stream_frame_;
353   QuicConnectionCloseFrame connection_close_frame_;
354   QuicGoAwayFrame goaway_frame_;
355   QuicWindowUpdateFrame window_update_frame_;
356   QuicBlockedFrame blocked_frame_;
357 };
358
359 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
360  public:
361   QuicFramerTest()
362       : encrypter_(new test::TestEncrypter()),
363         decrypter_(new test::TestDecrypter()),
364         start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
365         framer_(QuicSupportedVersions(), start_, true) {
366     version_ = GetParam();
367     framer_.set_version(version_);
368     framer_.SetDecrypter(decrypter_);
369     framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
370     framer_.set_visitor(&visitor_);
371     framer_.set_received_entropy_calculator(&entropy_calculator_);
372   }
373
374   // Helper function to get unsigned char representation of digit in the
375   // units place of the current QUIC version number.
376   unsigned char GetQuicVersionDigitOnes() {
377     return static_cast<unsigned char> ('0' + version_%10);
378   }
379
380   // Helper function to get unsigned char representation of digit in the
381   // tens place of the current QUIC version number.
382   unsigned char GetQuicVersionDigitTens() {
383     return static_cast<unsigned char> ('0' + (version_/10)%10);
384   }
385
386   bool CheckEncryption(QuicPacketSequenceNumber sequence_number,
387                        QuicPacket* packet) {
388     if (sequence_number != encrypter_->sequence_number_) {
389       LOG(ERROR) << "Encrypted incorrect packet sequence number.  expected "
390                  << sequence_number << " actual: "
391                  << encrypter_->sequence_number_;
392       return false;
393     }
394     if (packet->AssociatedData() != encrypter_->associated_data_) {
395       LOG(ERROR) << "Encrypted incorrect associated data.  expected "
396                  << packet->AssociatedData() << " actual: "
397                  << encrypter_->associated_data_;
398       return false;
399     }
400     if (packet->Plaintext() != encrypter_->plaintext_) {
401       LOG(ERROR) << "Encrypted incorrect plaintext data.  expected "
402                  << packet->Plaintext() << " actual: "
403                  << encrypter_->plaintext_;
404       return false;
405     }
406     return true;
407   }
408
409   bool CheckDecryption(const QuicEncryptedPacket& encrypted,
410                        bool includes_version) {
411     if (visitor_.header_->packet_sequence_number !=
412         decrypter_->sequence_number_) {
413       LOG(ERROR) << "Decrypted incorrect packet sequence number.  expected "
414                  << visitor_.header_->packet_sequence_number << " actual: "
415                  << decrypter_->sequence_number_;
416       return false;
417     }
418     if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
419             encrypted, PACKET_8BYTE_CONNECTION_ID,
420             includes_version, PACKET_6BYTE_SEQUENCE_NUMBER) !=
421         decrypter_->associated_data_) {
422       LOG(ERROR) << "Decrypted incorrect associated data.  expected "
423                  << QuicFramer::GetAssociatedDataFromEncryptedPacket(
424                      encrypted, PACKET_8BYTE_CONNECTION_ID,
425                      includes_version, PACKET_6BYTE_SEQUENCE_NUMBER)
426                  << " actual: " << decrypter_->associated_data_;
427       return false;
428     }
429     StringPiece ciphertext(encrypted.AsStringPiece().substr(
430         GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID, includes_version,
431                                 PACKET_6BYTE_SEQUENCE_NUMBER)));
432     if (ciphertext != decrypter_->ciphertext_) {
433       LOG(ERROR) << "Decrypted incorrect ciphertext data.  expected "
434                  << ciphertext << " actual: "
435                  << decrypter_->ciphertext_;
436       return false;
437     }
438     return true;
439   }
440
441   char* AsChars(unsigned char* data) {
442     return reinterpret_cast<char*>(data);
443   }
444
445   void CheckProcessingFails(unsigned char* packet,
446                             size_t len,
447                             string expected_error,
448                             QuicErrorCode error_code) {
449     QuicEncryptedPacket encrypted(AsChars(packet), len, false);
450     EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
451     EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
452     EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
453   }
454
455   // Checks if the supplied string matches data in the supplied StreamFrame.
456   void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
457     scoped_ptr<string> frame_data(frame->GetDataAsString());
458     EXPECT_EQ(str, *frame_data);
459   }
460
461   void CheckStreamFrameBoundaries(unsigned char* packet,
462                                   size_t stream_id_size,
463                                   bool include_version) {
464     // Now test framing boundaries
465     for (size_t i = kQuicFrameTypeSize;
466          i < GetMinStreamFrameSize(framer_.version()); ++i) {
467       string expected_error;
468       if (i < kQuicFrameTypeSize + stream_id_size) {
469         expected_error = "Unable to read stream_id.";
470       } else if (i < kQuicFrameTypeSize + stream_id_size +
471                  kQuicMaxStreamOffsetSize) {
472         expected_error = "Unable to read offset.";
473       } else {
474         expected_error = "Unable to read frame data.";
475       }
476       CheckProcessingFails(
477           packet,
478           i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
479                                   PACKET_6BYTE_SEQUENCE_NUMBER,
480                                   NOT_IN_FEC_GROUP),
481           expected_error, QUIC_INVALID_STREAM_DATA);
482     }
483   }
484
485   void CheckCalculatePacketSequenceNumber(
486       QuicPacketSequenceNumber expected_sequence_number,
487       QuicPacketSequenceNumber last_sequence_number) {
488     QuicPacketSequenceNumber wire_sequence_number =
489         expected_sequence_number & kMask;
490     QuicFramerPeer::SetLastSequenceNumber(&framer_, last_sequence_number);
491     EXPECT_EQ(expected_sequence_number,
492               QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
493                   &framer_, PACKET_6BYTE_SEQUENCE_NUMBER, wire_sequence_number))
494         << "last_sequence_number: " << last_sequence_number
495         << " wire_sequence_number: " << wire_sequence_number;
496   }
497
498   test::TestEncrypter* encrypter_;
499   test::TestDecrypter* decrypter_;
500   QuicVersion version_;
501   QuicTime start_;
502   QuicFramer framer_;
503   test::TestQuicVisitor visitor_;
504   test::TestEntropyCalculator entropy_calculator_;
505 };
506
507 // Run all framer tests with all supported versions of QUIC.
508 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
509                         QuicFramerTest,
510                         ::testing::ValuesIn(kSupportedQuicVersions));
511
512 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochStart) {
513   // A few quick manual sanity checks
514   CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
515   CheckCalculatePacketSequenceNumber(kEpoch + 1, kMask);
516   CheckCalculatePacketSequenceNumber(kEpoch, kMask);
517
518   // Cases where the last number was close to the start of the range
519   for (uint64 last = 0; last < 10; last++) {
520     // Small numbers should not wrap (even if they're out of order).
521     for (uint64 j = 0; j < 10; j++) {
522       CheckCalculatePacketSequenceNumber(j, last);
523     }
524
525     // Large numbers should not wrap either (because we're near 0 already).
526     for (uint64 j = 0; j < 10; j++) {
527       CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
528     }
529   }
530 }
531
532 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochEnd) {
533   // Cases where the last number was close to the end of the range
534   for (uint64 i = 0; i < 10; i++) {
535     QuicPacketSequenceNumber last = kEpoch - i;
536
537     // Small numbers should wrap.
538     for (uint64 j = 0; j < 10; j++) {
539       CheckCalculatePacketSequenceNumber(kEpoch + j, last);
540     }
541
542     // Large numbers should not (even if they're out of order).
543     for (uint64 j = 0; j < 10; j++) {
544       CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
545     }
546   }
547 }
548
549 // Next check where we're in a non-zero epoch to verify we handle
550 // reverse wrapping, too.
551 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearPrevEpoch) {
552   const uint64 prev_epoch = 1 * kEpoch;
553   const uint64 cur_epoch = 2 * kEpoch;
554   // Cases where the last number was close to the start of the range
555   for (uint64 i = 0; i < 10; i++) {
556     uint64 last = cur_epoch + i;
557     // Small number should not wrap (even if they're out of order).
558     for (uint64 j = 0; j < 10; j++) {
559       CheckCalculatePacketSequenceNumber(cur_epoch + j, last);
560     }
561
562     // But large numbers should reverse wrap.
563     for (uint64 j = 0; j < 10; j++) {
564       uint64 num = kEpoch - 1 - j;
565       CheckCalculatePacketSequenceNumber(prev_epoch + num, last);
566     }
567   }
568 }
569
570 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextEpoch) {
571   const uint64 cur_epoch = 2 * kEpoch;
572   const uint64 next_epoch = 3 * kEpoch;
573   // Cases where the last number was close to the end of the range
574   for (uint64 i = 0; i < 10; i++) {
575     QuicPacketSequenceNumber last = next_epoch - 1 - i;
576
577     // Small numbers should wrap.
578     for (uint64 j = 0; j < 10; j++) {
579       CheckCalculatePacketSequenceNumber(next_epoch + j, last);
580     }
581
582     // but large numbers should not (even if they're out of order).
583     for (uint64 j = 0; j < 10; j++) {
584       uint64 num = kEpoch - 1 - j;
585       CheckCalculatePacketSequenceNumber(cur_epoch + num, last);
586     }
587   }
588 }
589
590 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
591   const uint64 max_number = numeric_limits<uint64>::max();
592   const uint64 max_epoch = max_number & ~kMask;
593
594   // Cases where the last number was close to the end of the range
595   for (uint64 i = 0; i < 10; i++) {
596     // Subtract 1, because the expected next sequence number is 1 more than the
597     // last sequence number.
598     QuicPacketSequenceNumber last = max_number - i - 1;
599
600     // Small numbers should not wrap, because they have nowhere to go.
601     for (uint64 j = 0; j < 10; j++) {
602       CheckCalculatePacketSequenceNumber(max_epoch + j, last);
603     }
604
605     // Large numbers should not wrap either.
606     for (uint64 j = 0; j < 10; j++) {
607       uint64 num = kEpoch - 1 - j;
608       CheckCalculatePacketSequenceNumber(max_epoch + num, last);
609     }
610   }
611 }
612
613 TEST_P(QuicFramerTest, EmptyPacket) {
614   char packet[] = { 0x00 };
615   QuicEncryptedPacket encrypted(packet, 0, false);
616   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
617   EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
618 }
619
620 TEST_P(QuicFramerTest, LargePacket) {
621   unsigned char packet[kMaxPacketSize + 1] = {
622     // public flags (8 byte connection_id)
623     0x3C,
624     // connection_id
625     0x10, 0x32, 0x54, 0x76,
626     0x98, 0xBA, 0xDC, 0xFE,
627     // packet sequence number
628     0xBC, 0x9A, 0x78, 0x56,
629     0x34, 0x12,
630     // private flags
631     0x00,
632   };
633
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);
640
641   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
642   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
643
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());
650 }
651
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,
664   };
665
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);
681
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.";
698     }
699     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
700   }
701 }
702
703 TEST_P(QuicFramerTest, PacketHeaderWith4ByteConnectionId) {
704   QuicFramerPeer::SetLastSerializedConnectionId(
705       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
706
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,
717   };
718
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);
734
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.";
754     }
755     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
756   }
757 }
758
759 TEST_P(QuicFramerTest, PacketHeader1ByteConnectionId) {
760   QuicFramerPeer::SetLastSerializedConnectionId(
761       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
762
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,
773   };
774
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);
790
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.";
810     }
811     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
812   }
813 }
814
815 TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
816   QuicFramerPeer::SetLastSerializedConnectionId(
817       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
818
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,
828   };
829
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);
845
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.";
865     }
866     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
867   }
868 }
869
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,
884   };
885
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);
902
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.";
921     }
922     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
923   }
924 }
925
926 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
927   QuicFramerPeer::SetLastSequenceNumber(&framer_,
928                                         GG_UINT64_C(0x123456789ABA));
929
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,
940   };
941
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);
957
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.";
976     }
977     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
978   }
979 }
980
981 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
982   QuicFramerPeer::SetLastSequenceNumber(&framer_,
983                                         GG_UINT64_C(0x123456789ABA));
984
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,
995   };
996
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);
1012
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.";
1031     }
1032     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1033   }
1034 }
1035
1036 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
1037   QuicFramerPeer::SetLastSequenceNumber(&framer_,
1038                                         GG_UINT64_C(0x123456789ABA));
1039
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,
1050   };
1051
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);
1067
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.";
1086     }
1087     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1088   }
1089 }
1090
1091 TEST_P(QuicFramerTest, InvalidPublicFlag) {
1092   unsigned char packet[] = {
1093     // public flags, unknown flag at bit 6
1094     0x40,
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,
1103
1104     // frame type (padding)
1105     0x00,
1106     0x00, 0x00, 0x00, 0x00
1107   };
1108   CheckProcessingFails(packet,
1109                        arraysize(packet),
1110                        "Illegal public flags value.",
1111                        QUIC_INVALID_PACKET_HEADER);
1112 };
1113
1114 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
1115   unsigned char packet[] = {
1116     // public flags (8 byte connection_id and version flag and an unknown flag)
1117     0x4D,
1118     // connection_id
1119     0x10, 0x32, 0x54, 0x76,
1120     0x98, 0xBA, 0xDC, 0xFE,
1121     // version tag
1122     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1123     // packet sequence number
1124     0xBC, 0x9A, 0x78, 0x56,
1125     0x34, 0x12,
1126     // private flags
1127     0x00,
1128
1129     // frame type (padding)
1130     0x00,
1131     0x00, 0x00, 0x00, 0x00
1132   };
1133   CheckProcessingFails(packet,
1134                        arraysize(packet),
1135                        "Illegal public flags value.",
1136                        QUIC_INVALID_PACKET_HEADER);
1137 };
1138
1139 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1140   unsigned char packet[] = {
1141     // public flags (8 byte connection_id, version flag and an unknown flag)
1142     0x7D,
1143     // connection_id
1144     0x10, 0x32, 0x54, 0x76,
1145     0x98, 0xBA, 0xDC, 0xFE,
1146     // version tag
1147     'Q', '0', '0', '0',
1148     // packet sequence number
1149     0xBC, 0x9A, 0x78, 0x56,
1150     0x34, 0x12,
1151     // private flags
1152     0x00,
1153
1154     // frame type (padding frame)
1155     0x00,
1156     0x00, 0x00, 0x00, 0x00
1157   };
1158   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1159   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1160   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1161   ASSERT_TRUE(visitor_.header_.get());
1162   EXPECT_EQ(0, visitor_.frame_count_);
1163   EXPECT_EQ(1, visitor_.version_mismatch_);
1164 };
1165
1166 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
1167   unsigned char packet[] = {
1168     // public flags (8 byte connection_id)
1169     0x3C,
1170     // connection_id
1171     0x10, 0x32, 0x54, 0x76,
1172     0x98, 0xBA, 0xDC, 0xFE,
1173     // packet sequence number
1174     0xBC, 0x9A, 0x78, 0x56,
1175     0x34, 0x12,
1176     // private flags
1177     0x10,
1178
1179     // frame type (padding)
1180     0x00,
1181     0x00, 0x00, 0x00, 0x00
1182   };
1183   CheckProcessingFails(packet,
1184                        arraysize(packet),
1185                        "Illegal private flags value.",
1186                        QUIC_INVALID_PACKET_HEADER);
1187 };
1188
1189 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
1190   unsigned char packet[] = {
1191     // public flags (8 byte connection_id)
1192     0x3C,
1193     // connection_id
1194     0x10, 0x32, 0x54, 0x76,
1195     0x98, 0xBA, 0xDC, 0xFE,
1196     // packet sequence number
1197     0x01, 0x00, 0x00, 0x00,
1198     0x00, 0x00,
1199     // private flags (fec group)
1200     0x02,
1201     // first fec protected packet offset
1202     0x10
1203   };
1204   CheckProcessingFails(packet,
1205                        arraysize(packet),
1206                        "First fec protected packet offset must be less "
1207                        "than the sequence number.",
1208                        QUIC_INVALID_PACKET_HEADER);
1209 };
1210
1211 TEST_P(QuicFramerTest, PaddingFrame) {
1212   unsigned char packet[] = {
1213     // public flags (8 byte connection_id)
1214     0x3C,
1215     // connection_id
1216     0x10, 0x32, 0x54, 0x76,
1217     0x98, 0xBA, 0xDC, 0xFE,
1218     // packet sequence number
1219     0xBC, 0x9A, 0x78, 0x56,
1220     0x34, 0x12,
1221     // private flags
1222     0x00,
1223
1224     // frame type (padding frame)
1225     0x00,
1226     // Ignored data (which in this case is a stream frame)
1227     // frame type (stream frame with fin)
1228     0xFF,
1229     // stream id
1230     0x04, 0x03, 0x02, 0x01,
1231     // offset
1232     0x54, 0x76, 0x10, 0x32,
1233     0xDC, 0xFE, 0x98, 0xBA,
1234     // data length
1235     0x0c, 0x00,
1236     // data
1237     'h',  'e',  'l',  'l',
1238     'o',  ' ',  'w',  'o',
1239     'r',  'l',  'd',  '!',
1240   };
1241
1242   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1243   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1244   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1245   ASSERT_TRUE(visitor_.header_.get());
1246   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1247
1248   ASSERT_EQ(0u, visitor_.stream_frames_.size());
1249   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1250   // A packet with no frames is not acceptable.
1251   CheckProcessingFails(
1252       packet,
1253       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1254                           PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1255       "Packet has no frames.", QUIC_MISSING_PAYLOAD);
1256 }
1257
1258 TEST_P(QuicFramerTest, StreamFrame) {
1259   unsigned char packet[] = {
1260     // public flags (8 byte connection_id)
1261     0x3C,
1262     // connection_id
1263     0x10, 0x32, 0x54, 0x76,
1264     0x98, 0xBA, 0xDC, 0xFE,
1265     // packet sequence number
1266     0xBC, 0x9A, 0x78, 0x56,
1267     0x34, 0x12,
1268     // private flags
1269     0x00,
1270
1271     // frame type (stream frame with fin)
1272     0xFF,
1273     // stream id
1274     0x04, 0x03, 0x02, 0x01,
1275     // offset
1276     0x54, 0x76, 0x10, 0x32,
1277     0xDC, 0xFE, 0x98, 0xBA,
1278     // data length
1279     0x0c, 0x00,
1280     // data
1281     'h',  'e',  'l',  'l',
1282     'o',  ' ',  'w',  'o',
1283     'r',  'l',  'd',  '!',
1284   };
1285
1286   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1287   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1288
1289   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1290   ASSERT_TRUE(visitor_.header_.get());
1291   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1292
1293   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1294   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1295   EXPECT_EQ(static_cast<uint64>(0x01020304),
1296             visitor_.stream_frames_[0]->stream_id);
1297   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1298   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1299             visitor_.stream_frames_[0]->offset);
1300   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1301
1302   // Now test framing boundaries
1303   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
1304 }
1305
1306 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
1307   unsigned char packet[] = {
1308     // public flags (8 byte connection_id)
1309     0x3C,
1310     // connection_id
1311     0x10, 0x32, 0x54, 0x76,
1312     0x98, 0xBA, 0xDC, 0xFE,
1313     // packet sequence number
1314     0xBC, 0x9A, 0x78, 0x56,
1315     0x34, 0x12,
1316     // private flags
1317     0x00,
1318
1319     // frame type (stream frame with fin)
1320     0xFE,
1321     // stream id
1322     0x04, 0x03, 0x02,
1323     // offset
1324     0x54, 0x76, 0x10, 0x32,
1325     0xDC, 0xFE, 0x98, 0xBA,
1326     // data length
1327     0x0c, 0x00,
1328     // data
1329     'h',  'e',  'l',  'l',
1330     'o',  ' ',  'w',  'o',
1331     'r',  'l',  'd',  '!',
1332   };
1333
1334   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1335   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1336
1337   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1338   ASSERT_TRUE(visitor_.header_.get());
1339   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1340
1341   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1342   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1343   EXPECT_EQ(GG_UINT64_C(0x00020304),
1344             visitor_.stream_frames_[0]->stream_id);
1345   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1346   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1347             visitor_.stream_frames_[0]->offset);
1348   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1349
1350   // Now test framing boundaries
1351   const size_t stream_id_size = 3;
1352   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1353 }
1354
1355 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
1356   unsigned char packet[] = {
1357     // public flags (8 byte connection_id)
1358     0x3C,
1359     // connection_id
1360     0x10, 0x32, 0x54, 0x76,
1361     0x98, 0xBA, 0xDC, 0xFE,
1362     // packet sequence number
1363     0xBC, 0x9A, 0x78, 0x56,
1364     0x34, 0x12,
1365     // private flags
1366     0x00,
1367
1368     // frame type (stream frame with fin)
1369     0xFD,
1370     // stream id
1371     0x04, 0x03,
1372     // offset
1373     0x54, 0x76, 0x10, 0x32,
1374     0xDC, 0xFE, 0x98, 0xBA,
1375     // data length
1376     0x0c, 0x00,
1377     // data
1378     'h',  'e',  'l',  'l',
1379     'o',  ' ',  'w',  'o',
1380     'r',  'l',  'd',  '!',
1381   };
1382
1383   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1384   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1385
1386   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1387   ASSERT_TRUE(visitor_.header_.get());
1388   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1389
1390   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1391   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1392   EXPECT_EQ(static_cast<uint64>(0x00000304),
1393             visitor_.stream_frames_[0]->stream_id);
1394   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1395   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1396             visitor_.stream_frames_[0]->offset);
1397   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1398
1399   // Now test framing boundaries
1400   const size_t stream_id_size = 2;
1401   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1402 }
1403
1404 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
1405   unsigned char packet[] = {
1406     // public flags (8 byte connection_id)
1407     0x3C,
1408     // connection_id
1409     0x10, 0x32, 0x54, 0x76,
1410     0x98, 0xBA, 0xDC, 0xFE,
1411     // packet sequence number
1412     0xBC, 0x9A, 0x78, 0x56,
1413     0x34, 0x12,
1414     // private flags
1415     0x00,
1416
1417     // frame type (stream frame with fin)
1418     0xFC,
1419     // stream id
1420     0x04,
1421     // offset
1422     0x54, 0x76, 0x10, 0x32,
1423     0xDC, 0xFE, 0x98, 0xBA,
1424     // data length
1425     0x0c, 0x00,
1426     // data
1427     'h',  'e',  'l',  'l',
1428     'o',  ' ',  'w',  'o',
1429     'r',  'l',  'd',  '!',
1430   };
1431
1432   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1433   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1434
1435   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1436   ASSERT_TRUE(visitor_.header_.get());
1437   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1438
1439   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1440   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1441   EXPECT_EQ(static_cast<uint64>(0x00000004),
1442             visitor_.stream_frames_[0]->stream_id);
1443   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1444   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1445             visitor_.stream_frames_[0]->offset);
1446   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1447
1448   // Now test framing boundaries
1449   const size_t stream_id_size = 1;
1450   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1451 }
1452
1453 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
1454   unsigned char packet[] = {
1455     // public flags (version, 8 byte connection_id)
1456     0x3D,
1457     // connection_id
1458     0x10, 0x32, 0x54, 0x76,
1459     0x98, 0xBA, 0xDC, 0xFE,
1460     // version tag
1461     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1462     // packet sequence number
1463     0xBC, 0x9A, 0x78, 0x56,
1464     0x34, 0x12,
1465     // private flags
1466     0x00,
1467
1468     // frame type (stream frame with fin)
1469     0xFF,
1470     // stream id
1471     0x04, 0x03, 0x02, 0x01,
1472     // offset
1473     0x54, 0x76, 0x10, 0x32,
1474     0xDC, 0xFE, 0x98, 0xBA,
1475     // data length
1476     0x0c, 0x00,
1477     // data
1478     'h',  'e',  'l',  'l',
1479     'o',  ' ',  'w',  'o',
1480     'r',  'l',  'd',  '!',
1481   };
1482
1483   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1484   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1485
1486   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1487   ASSERT_TRUE(visitor_.header_.get());
1488   EXPECT_TRUE(visitor_.header_.get()->public_header.version_flag);
1489   EXPECT_EQ(GetParam(), visitor_.header_.get()->public_header.versions[0]);
1490   EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
1491
1492   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1493   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1494   EXPECT_EQ(static_cast<uint64>(0x01020304),
1495             visitor_.stream_frames_[0]->stream_id);
1496   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1497   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1498             visitor_.stream_frames_[0]->offset);
1499   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1500
1501   // Now test framing boundaries
1502   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
1503 }
1504
1505 TEST_P(QuicFramerTest, RejectPacket) {
1506   visitor_.accept_packet_ = false;
1507
1508   unsigned char packet[] = {
1509     // public flags (8 byte connection_id)
1510     0x3C,
1511     // connection_id
1512     0x10, 0x32, 0x54, 0x76,
1513     0x98, 0xBA, 0xDC, 0xFE,
1514     // packet sequence number
1515     0xBC, 0x9A, 0x78, 0x56,
1516     0x34, 0x12,
1517     // private flags
1518     0x00,
1519
1520     // frame type (stream frame with fin)
1521     0xFF,
1522     // stream id
1523     0x04, 0x03, 0x02, 0x01,
1524     // offset
1525     0x54, 0x76, 0x10, 0x32,
1526     0xDC, 0xFE, 0x98, 0xBA,
1527     // data length
1528     0x0c, 0x00,
1529     // data
1530     'h',  'e',  'l',  'l',
1531     'o',  ' ',  'w',  'o',
1532     'r',  'l',  'd',  '!',
1533   };
1534
1535   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1536   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1537
1538   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1539   ASSERT_TRUE(visitor_.header_.get());
1540   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1541
1542   ASSERT_EQ(0u, visitor_.stream_frames_.size());
1543   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1544 }
1545
1546 TEST_P(QuicFramerTest, RejectPublicHeader) {
1547   visitor_.accept_public_header_ = false;
1548
1549   unsigned char packet[] = {
1550     // public flags (8 byte connection_id)
1551     0x3C,
1552     // connection_id
1553     0x10, 0x32, 0x54, 0x76,
1554     0x98, 0xBA, 0xDC, 0xFE,
1555   };
1556
1557   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1558   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1559
1560   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1561   ASSERT_TRUE(visitor_.public_header_.get());
1562   ASSERT_FALSE(visitor_.header_.get());
1563 }
1564
1565 TEST_P(QuicFramerTest, RevivedStreamFrame) {
1566   unsigned char payload[] = {
1567     // frame type (stream frame with fin)
1568     0xFF,
1569     // stream id
1570     0x04, 0x03, 0x02, 0x01,
1571     // offset
1572     0x54, 0x76, 0x10, 0x32,
1573     0xDC, 0xFE, 0x98, 0xBA,
1574     // data length
1575     0x0c, 0x00,
1576     // data
1577     'h',  'e',  'l',  'l',
1578     'o',  ' ',  'w',  'o',
1579     'r',  'l',  'd',  '!',
1580   };
1581
1582   QuicPacketHeader header;
1583   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
1584   header.public_header.reset_flag = false;
1585   header.public_header.version_flag = false;
1586   header.fec_flag = true;
1587   header.entropy_flag = true;
1588   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
1589   header.fec_group = 0;
1590
1591   // Do not encrypt the payload because the revived payload is post-encryption.
1592   EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
1593                                            StringPiece(AsChars(payload),
1594                                                        arraysize(payload))));
1595
1596   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1597   ASSERT_EQ(1, visitor_.revived_packets_);
1598   ASSERT_TRUE(visitor_.header_.get());
1599   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1600             visitor_.header_->public_header.connection_id);
1601   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1602   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1603   EXPECT_TRUE(visitor_.header_->fec_flag);
1604   EXPECT_TRUE(visitor_.header_->entropy_flag);
1605   EXPECT_EQ(1 << (header.packet_sequence_number % 8),
1606             visitor_.header_->entropy_hash);
1607   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1608             visitor_.header_->packet_sequence_number);
1609   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1610   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1611
1612   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1613   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1614   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1615   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1616   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1617             visitor_.stream_frames_[0]->offset);
1618   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1619 }
1620
1621 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
1622   unsigned char packet[] = {
1623     // public flags (8 byte connection_id)
1624     0x3C,
1625     // connection_id
1626     0x10, 0x32, 0x54, 0x76,
1627     0x98, 0xBA, 0xDC, 0xFE,
1628     // packet sequence number
1629     0xBC, 0x9A, 0x78, 0x56,
1630     0x12, 0x34,
1631     // private flags (fec group)
1632     0x02,
1633     // first fec protected packet offset
1634     0x02,
1635
1636     // frame type (stream frame with fin)
1637     0xFF,
1638     // stream id
1639     0x04, 0x03, 0x02, 0x01,
1640     // offset
1641     0x54, 0x76, 0x10, 0x32,
1642     0xDC, 0xFE, 0x98, 0xBA,
1643     // data length
1644     0x0c, 0x00,
1645     // data
1646     'h',  'e',  'l',  'l',
1647     'o',  ' ',  'w',  'o',
1648     'r',  'l',  'd',  '!',
1649   };
1650
1651   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1652   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1653
1654   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1655   ASSERT_TRUE(visitor_.header_.get());
1656   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1657   EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1658   EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
1659             visitor_.header_->fec_group);
1660   const size_t fec_offset =
1661       GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID,
1662                                  !kIncludeVersion,
1663                                  PACKET_6BYTE_SEQUENCE_NUMBER);
1664   EXPECT_EQ(
1665       string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
1666       visitor_.fec_protected_payload_);
1667
1668   ASSERT_EQ(1u, visitor_.stream_frames_.size());
1669   EXPECT_EQ(0u, visitor_.ack_frames_.size());
1670   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1671   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1672   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1673             visitor_.stream_frames_[0]->offset);
1674   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1675 }
1676
1677 TEST_P(QuicFramerTest, AckFrameV13) {
1678   if (framer_.version() != QUIC_VERSION_13) {
1679     return;
1680   }
1681
1682   unsigned char packet[] = {
1683     // public flags (8 byte connection_id)
1684     0x3C,
1685     // connection_id
1686     0x10, 0x32, 0x54, 0x76,
1687     0x98, 0xBA, 0xDC, 0xFE,
1688     // packet sequence number
1689     0xA8, 0x9A, 0x78, 0x56,
1690     0x34, 0x12,
1691     // private flags (entropy)
1692     0x01,
1693
1694     // frame type (ack frame)
1695     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1696     0x6C,
1697     // entropy hash of sent packets till least awaiting - 1.
1698     0xAB,
1699     // least packet sequence number awaiting an ack, delta from sequence number.
1700     0x08, 0x00, 0x00, 0x00,
1701     0x00, 0x00,
1702     // entropy hash of all received packets.
1703     0xBA,
1704     // largest observed packet sequence number
1705     0xBF, 0x9A, 0x78, 0x56,
1706     0x34, 0x12,
1707     // Zero delta time.
1708     0x0, 0x0,
1709     // num missing packets
1710     0x01,
1711     // missing packet delta
1712     0x01,
1713     // 0 more missing packets in range.
1714     0x00,
1715   };
1716
1717   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1718   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1719
1720   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1721   ASSERT_TRUE(visitor_.header_.get());
1722   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1723
1724   EXPECT_EQ(0u, visitor_.stream_frames_.size());
1725   ASSERT_EQ(1u, visitor_.ack_frames_.size());
1726   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1727   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
1728   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
1729   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
1730   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
1731   SequenceNumberSet::const_iterator missing_iter =
1732       frame.received_info.missing_packets.begin();
1733   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1734   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
1735
1736   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
1737   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
1738   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
1739       PACKET_6BYTE_SEQUENCE_NUMBER;
1740   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1741       kQuicEntropyHashSize;
1742   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1743       PACKET_6BYTE_SEQUENCE_NUMBER;
1744   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1745       kQuicDeltaTimeLargestObservedSize;
1746   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1747       kNumberOfMissingPacketsSize;
1748   const size_t kMissingPacketsRange = kMissingPacketsOffset +
1749       PACKET_1BYTE_SEQUENCE_NUMBER;
1750   // Now test framing boundaries
1751   const size_t ack_frame_size = kMissingPacketsRange +
1752       PACKET_1BYTE_SEQUENCE_NUMBER;
1753   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1754     string expected_error;
1755     if (i < kLeastUnackedOffset) {
1756       expected_error = "Unable to read entropy hash for sent packets.";
1757     } else if (i < kReceivedEntropyOffset) {
1758       expected_error = "Unable to read least unacked delta.";
1759     } else if (i < kLargestObservedOffset) {
1760       expected_error = "Unable to read entropy hash for received packets.";
1761     } else if (i < kMissingDeltaTimeOffset) {
1762       expected_error = "Unable to read largest observed.";
1763     } else if (i < kNumMissingPacketOffset) {
1764       expected_error = "Unable to read delta time largest observed.";
1765     } else if (i < kMissingPacketsOffset) {
1766       expected_error = "Unable to read num missing packet ranges.";
1767     } else if (i < kMissingPacketsRange) {
1768       expected_error = "Unable to read missing sequence number delta.";
1769     } else {
1770       expected_error = "Unable to read missing sequence number range.";
1771     }
1772     CheckProcessingFails(
1773         packet,
1774         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1775                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1776         expected_error, QUIC_INVALID_ACK_DATA);
1777   }
1778 }
1779
1780 TEST_P(QuicFramerTest, AckFrame15) {
1781   if (framer_.version() != QUIC_VERSION_15) {
1782     return;
1783   }
1784
1785   unsigned char packet[] = {
1786     // public flags (8 byte connection_id)
1787     0x3C,
1788     // connection_id
1789     0x10, 0x32, 0x54, 0x76,
1790     0x98, 0xBA, 0xDC, 0xFE,
1791     // packet sequence number
1792     0xA8, 0x9A, 0x78, 0x56,
1793     0x34, 0x12,
1794     // private flags (entropy)
1795     0x01,
1796
1797     // frame type (ack frame)
1798     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1799     0x6C,
1800     // entropy hash of sent packets till least awaiting - 1.
1801     0xAB,
1802     // least packet sequence number awaiting an ack, delta from sequence number.
1803     0x08, 0x00, 0x00, 0x00,
1804     0x00, 0x00,
1805     // entropy hash of all received packets.
1806     0xBA,
1807     // largest observed packet sequence number
1808     0xBF, 0x9A, 0x78, 0x56,
1809     0x34, 0x12,
1810     // Zero delta time.
1811     0x0, 0x0,
1812     // num missing packets
1813     0x01,
1814     // missing packet delta
1815     0x01,
1816     // 0 more missing packets in range.
1817     0x00,
1818     // Number of revived packets.
1819     0x00,
1820   };
1821
1822   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1823   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1824
1825   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1826   ASSERT_TRUE(visitor_.header_.get());
1827   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1828
1829   EXPECT_EQ(0u, visitor_.stream_frames_.size());
1830   ASSERT_EQ(1u, visitor_.ack_frames_.size());
1831   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1832   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
1833   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
1834   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
1835   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
1836   SequenceNumberSet::const_iterator missing_iter =
1837       frame.received_info.missing_packets.begin();
1838   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1839   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
1840
1841   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
1842   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
1843   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
1844       PACKET_6BYTE_SEQUENCE_NUMBER;
1845   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1846       kQuicEntropyHashSize;
1847   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1848       PACKET_6BYTE_SEQUENCE_NUMBER;
1849   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1850       kQuicDeltaTimeLargestObservedSize;
1851   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1852       kNumberOfMissingPacketsSize;
1853   const size_t kMissingPacketsRange = kMissingPacketsOffset +
1854       PACKET_1BYTE_SEQUENCE_NUMBER;
1855   const size_t kRevivedPacketsLength = kMissingPacketsRange +
1856       PACKET_1BYTE_SEQUENCE_NUMBER;
1857   // Now test framing boundaries
1858   const size_t ack_frame_size = kRevivedPacketsLength +
1859       PACKET_1BYTE_SEQUENCE_NUMBER;
1860   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1861     string expected_error;
1862     if (i < kLeastUnackedOffset) {
1863       expected_error = "Unable to read entropy hash for sent packets.";
1864     } else if (i < kReceivedEntropyOffset) {
1865       expected_error = "Unable to read least unacked delta.";
1866     } else if (i < kLargestObservedOffset) {
1867       expected_error = "Unable to read entropy hash for received packets.";
1868     } else if (i < kMissingDeltaTimeOffset) {
1869       expected_error = "Unable to read largest observed.";
1870     } else if (i < kNumMissingPacketOffset) {
1871       expected_error = "Unable to read delta time largest observed.";
1872     } else if (i < kMissingPacketsOffset) {
1873       expected_error = "Unable to read num missing packet ranges.";
1874     } else if (i < kMissingPacketsRange) {
1875       expected_error = "Unable to read missing sequence number delta.";
1876     } else if (i < kRevivedPacketsLength) {
1877       expected_error = "Unable to read missing sequence number range.";
1878     } else {
1879       expected_error = "Unable to read num revived packets.";
1880     }
1881     CheckProcessingFails(
1882         packet,
1883         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1884                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1885         expected_error, QUIC_INVALID_ACK_DATA);
1886   }
1887 }
1888
1889 TEST_P(QuicFramerTest, AckFrame) {
1890   if (framer_.version() <= QUIC_VERSION_15) {
1891     return;
1892   }
1893
1894   unsigned char packet[] = {
1895     // public flags (8 byte connection_id)
1896     0x3C,
1897     // connection_id
1898     0x10, 0x32, 0x54, 0x76,
1899     0x98, 0xBA, 0xDC, 0xFE,
1900     // packet sequence number
1901     0xA8, 0x9A, 0x78, 0x56,
1902     0x34, 0x12,
1903     // private flags (entropy)
1904     0x01,
1905
1906     // frame type (ack frame)
1907     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1908     0x6C,
1909     // entropy hash of all received packets.
1910     0xBA,
1911     // largest observed packet sequence number
1912     0xBF, 0x9A, 0x78, 0x56,
1913     0x34, 0x12,
1914     // Zero delta time.
1915     0x0, 0x0,
1916     // num missing packets
1917     0x01,
1918     // missing packet delta
1919     0x01,
1920     // 0 more missing packets in range.
1921     0x00,
1922     // Number of revived packets.
1923     0x00,
1924   };
1925
1926   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1927   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1928
1929   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1930   ASSERT_TRUE(visitor_.header_.get());
1931   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1932
1933   EXPECT_EQ(0u, visitor_.stream_frames_.size());
1934   ASSERT_EQ(1u, visitor_.ack_frames_.size());
1935   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1936   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
1937   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
1938   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
1939   SequenceNumberSet::const_iterator missing_iter =
1940       frame.received_info.missing_packets.begin();
1941   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1942
1943   const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1944   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1945       kQuicEntropyHashSize;
1946   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1947       PACKET_6BYTE_SEQUENCE_NUMBER;
1948   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1949       kQuicDeltaTimeLargestObservedSize;
1950   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1951       kNumberOfMissingPacketsSize;
1952   const size_t kMissingPacketsRange = kMissingPacketsOffset +
1953       PACKET_1BYTE_SEQUENCE_NUMBER;
1954   const size_t kRevivedPacketsLength = kMissingPacketsRange +
1955       PACKET_1BYTE_SEQUENCE_NUMBER;
1956   // Now test framing boundaries
1957   const size_t ack_frame_size = kRevivedPacketsLength +
1958       PACKET_1BYTE_SEQUENCE_NUMBER;
1959   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1960     string expected_error;
1961     if (i < kLargestObservedOffset) {
1962       expected_error = "Unable to read entropy hash for received packets.";
1963     } else if (i < kMissingDeltaTimeOffset) {
1964       expected_error = "Unable to read largest observed.";
1965     } else if (i < kNumMissingPacketOffset) {
1966       expected_error = "Unable to read delta time largest observed.";
1967     } else if (i < kMissingPacketsOffset) {
1968       expected_error = "Unable to read num missing packet ranges.";
1969     } else if (i < kMissingPacketsRange) {
1970       expected_error = "Unable to read missing sequence number delta.";
1971     } else if (i < kRevivedPacketsLength) {
1972       expected_error = "Unable to read missing sequence number range.";
1973     } else {
1974       expected_error = "Unable to read num revived packets.";
1975     }
1976     CheckProcessingFails(
1977         packet,
1978         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1979                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1980         expected_error, QUIC_INVALID_ACK_DATA);
1981   }
1982 }
1983
1984 TEST_P(QuicFramerTest, AckFrameRevivedPackets) {
1985   if (framer_.version() <= QUIC_VERSION_15) {
1986     return;
1987   }
1988
1989   unsigned char packet[] = {
1990     // public flags (8 byte connection_id)
1991     0x3C,
1992     // connection_id
1993     0x10, 0x32, 0x54, 0x76,
1994     0x98, 0xBA, 0xDC, 0xFE,
1995     // packet sequence number
1996     0xA8, 0x9A, 0x78, 0x56,
1997     0x34, 0x12,
1998     // private flags (entropy)
1999     0x01,
2000
2001     // frame type (ack frame)
2002     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2003     0x6C,
2004     // entropy hash of all received packets.
2005     0xBA,
2006     // largest observed packet sequence number
2007     0xBF, 0x9A, 0x78, 0x56,
2008     0x34, 0x12,
2009     // Zero delta time.
2010     0x0, 0x0,
2011     // num missing packets
2012     0x01,
2013     // missing packet delta
2014     0x01,
2015     // 0 more missing packets in range.
2016     0x00,
2017     // Number of revived packets.
2018     0x01,
2019     // Revived packet sequence number.
2020     0xBE, 0x9A, 0x78, 0x56,
2021     0x34, 0x12,
2022   };
2023
2024   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2025   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2026
2027   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2028   ASSERT_TRUE(visitor_.header_.get());
2029   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2030
2031   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2032   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2033   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
2034   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
2035   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
2036   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
2037   SequenceNumberSet::const_iterator missing_iter =
2038       frame.received_info.missing_packets.begin();
2039   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
2040
2041   const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
2042   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
2043       kQuicEntropyHashSize;
2044   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
2045       PACKET_6BYTE_SEQUENCE_NUMBER;
2046   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
2047       kQuicDeltaTimeLargestObservedSize;
2048   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
2049       kNumberOfMissingPacketsSize;
2050   const size_t kMissingPacketsRange = kMissingPacketsOffset +
2051       PACKET_1BYTE_SEQUENCE_NUMBER;
2052   const size_t kRevivedPacketsLength = kMissingPacketsRange +
2053       PACKET_1BYTE_SEQUENCE_NUMBER;
2054   const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
2055       PACKET_1BYTE_SEQUENCE_NUMBER;
2056   // Now test framing boundaries
2057   const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
2058       PACKET_6BYTE_SEQUENCE_NUMBER;
2059   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
2060     string expected_error;
2061     if (i < kReceivedEntropyOffset) {
2062       expected_error = "Unable to read least unacked delta.";
2063     } else if (i < kLargestObservedOffset) {
2064       expected_error = "Unable to read entropy hash for received packets.";
2065     } else if (i < kMissingDeltaTimeOffset) {
2066       expected_error = "Unable to read largest observed.";
2067     } else if (i < kNumMissingPacketOffset) {
2068       expected_error = "Unable to read delta time largest observed.";
2069     } else if (i < kMissingPacketsOffset) {
2070       expected_error = "Unable to read num missing packet ranges.";
2071     } else if (i < kMissingPacketsRange) {
2072       expected_error = "Unable to read missing sequence number delta.";
2073     } else if (i < kRevivedPacketsLength) {
2074       expected_error = "Unable to read missing sequence number range.";
2075     } else if (i < kRevivedPacketSequenceNumberLength) {
2076       expected_error = "Unable to read num revived packets.";
2077     } else {
2078       expected_error = "Unable to read revived packet.";
2079     }
2080     CheckProcessingFails(
2081         packet,
2082         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2083                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2084         expected_error, QUIC_INVALID_ACK_DATA);
2085   }
2086 }
2087
2088 TEST_P(QuicFramerTest, AckFrameRevivedPackets15) {
2089   if (framer_.version() != QUIC_VERSION_15) {
2090     return;
2091   }
2092
2093   unsigned char packet[] = {
2094     // public flags (8 byte connection_id)
2095     0x3C,
2096     // connection_id
2097     0x10, 0x32, 0x54, 0x76,
2098     0x98, 0xBA, 0xDC, 0xFE,
2099     // packet sequence number
2100     0xA8, 0x9A, 0x78, 0x56,
2101     0x34, 0x12,
2102     // private flags (entropy)
2103     0x01,
2104
2105     // frame type (ack frame)
2106     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2107     0x6C,
2108     // entropy hash of sent packets till least awaiting - 1.
2109     0xAB,
2110     // least packet sequence number awaiting an ack, delta from sequence number.
2111     0x08, 0x00, 0x00, 0x00,
2112     0x00, 0x00,
2113     // entropy hash of all received packets.
2114     0xBA,
2115     // largest observed packet sequence number
2116     0xBF, 0x9A, 0x78, 0x56,
2117     0x34, 0x12,
2118     // Zero delta time.
2119     0x0, 0x0,
2120     // num missing packets
2121     0x01,
2122     // missing packet delta
2123     0x01,
2124     // 0 more missing packets in range.
2125     0x00,
2126     // Number of revived packets.
2127     0x01,
2128     // Revived packet sequence number.
2129     0xBE, 0x9A, 0x78, 0x56,
2130     0x34, 0x12,
2131   };
2132
2133   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2134   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2135
2136   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2137   ASSERT_TRUE(visitor_.header_.get());
2138   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2139
2140   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2141   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2142   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
2143   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
2144   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
2145   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
2146   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
2147   SequenceNumberSet::const_iterator missing_iter =
2148       frame.received_info.missing_packets.begin();
2149   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
2150   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
2151
2152   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
2153   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
2154   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
2155       PACKET_6BYTE_SEQUENCE_NUMBER;
2156   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
2157       kQuicEntropyHashSize;
2158   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
2159       PACKET_6BYTE_SEQUENCE_NUMBER;
2160   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
2161       kQuicDeltaTimeLargestObservedSize;
2162   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
2163       kNumberOfMissingPacketsSize;
2164   const size_t kMissingPacketsRange = kMissingPacketsOffset +
2165       PACKET_1BYTE_SEQUENCE_NUMBER;
2166   const size_t kRevivedPacketsLength = kMissingPacketsRange +
2167       PACKET_1BYTE_SEQUENCE_NUMBER;
2168   const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
2169       PACKET_1BYTE_SEQUENCE_NUMBER;
2170   // Now test framing boundaries
2171   const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
2172       PACKET_6BYTE_SEQUENCE_NUMBER;
2173   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
2174     string expected_error;
2175     if (i < kLeastUnackedOffset) {
2176       expected_error = "Unable to read entropy hash for sent packets.";
2177     } else if (i < kReceivedEntropyOffset) {
2178       expected_error = "Unable to read least unacked delta.";
2179     } else if (i < kLargestObservedOffset) {
2180       expected_error = "Unable to read entropy hash for received packets.";
2181     } else if (i < kMissingDeltaTimeOffset) {
2182       expected_error = "Unable to read largest observed.";
2183     } else if (i < kNumMissingPacketOffset) {
2184       expected_error = "Unable to read delta time largest observed.";
2185     } else if (i < kMissingPacketsOffset) {
2186       expected_error = "Unable to read num missing packet ranges.";
2187     } else if (i < kMissingPacketsRange) {
2188       expected_error = "Unable to read missing sequence number delta.";
2189     } else if (i < kRevivedPacketsLength) {
2190       expected_error = "Unable to read missing sequence number range.";
2191     } else if (i < kRevivedPacketSequenceNumberLength) {
2192       expected_error = "Unable to read num revived packets.";
2193     } else {
2194       expected_error = "Unable to read revived packet.";
2195     }
2196     CheckProcessingFails(
2197         packet,
2198         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2199                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2200         expected_error, QUIC_INVALID_ACK_DATA);
2201   }
2202 }
2203
2204 TEST_P(QuicFramerTest, AckFrameNoNacks) {
2205   if (framer_.version() <= QUIC_VERSION_15) {
2206     return;
2207   }
2208   unsigned char packet[] = {
2209     // public flags (8 byte connection_id)
2210     0x3C,
2211     // connection_id
2212     0x10, 0x32, 0x54, 0x76,
2213     0x98, 0xBA, 0xDC, 0xFE,
2214     // packet sequence number
2215     0xA8, 0x9A, 0x78, 0x56,
2216     0x34, 0x12,
2217     // private flags (entropy)
2218     0x01,
2219
2220     // frame type (ack frame)
2221     // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2222     0x4C,
2223     // entropy hash of all received packets.
2224     0xBA,
2225     // largest observed packet sequence number
2226     0xBF, 0x9A, 0x78, 0x56,
2227     0x34, 0x12,
2228     // Zero delta time.
2229     0x0, 0x0,
2230   };
2231
2232   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2233   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2234
2235   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2236   ASSERT_TRUE(visitor_.header_.get());
2237   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2238
2239   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2240   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2241   QuicAckFrame* frame = visitor_.ack_frames_[0];
2242   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
2243   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
2244             frame->received_info.largest_observed);
2245   ASSERT_EQ(0u, frame->received_info.missing_packets.size());
2246
2247   // Verify that the packet re-serializes identically.
2248   QuicFrames frames;
2249   frames.push_back(QuicFrame(frame));
2250   scoped_ptr<QuicPacket> data(
2251       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
2252   ASSERT_TRUE(data != NULL);
2253
2254   test::CompareCharArraysWithHexError("constructed packet",
2255                                       data->data(), data->length(),
2256                                       AsChars(packet), arraysize(packet));
2257 }
2258
2259 TEST_P(QuicFramerTest, AckFrameNoNacks15) {
2260   if (framer_.version() > QUIC_VERSION_15) {
2261     return;
2262   }
2263   unsigned char packet[] = {
2264     // public flags (8 byte connection_id)
2265     0x3C,
2266     // connection_id
2267     0x10, 0x32, 0x54, 0x76,
2268     0x98, 0xBA, 0xDC, 0xFE,
2269     // packet sequence number
2270     0xA8, 0x9A, 0x78, 0x56,
2271     0x34, 0x12,
2272     // private flags (entropy)
2273     0x01,
2274
2275     // frame type (ack frame)
2276     // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2277     0x4C,
2278     // entropy hash of sent packets till least awaiting - 1.
2279     0xAB,
2280     // least packet sequence number awaiting an ack, delta from sequence number.
2281     0x08, 0x00, 0x00, 0x00,
2282     0x00, 0x00,
2283     // entropy hash of all received packets.
2284     0xBA,
2285     // largest observed packet sequence number
2286     0xBF, 0x9A, 0x78, 0x56,
2287     0x34, 0x12,
2288     // Zero delta time.
2289     0x0, 0x0,
2290   };
2291
2292   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2293   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2294
2295   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2296   ASSERT_TRUE(visitor_.header_.get());
2297   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2298
2299   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2300   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2301   QuicAckFrame* frame = visitor_.ack_frames_[0];
2302   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
2303   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
2304   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
2305             frame->received_info.largest_observed);
2306   ASSERT_EQ(0u, frame->received_info.missing_packets.size());
2307   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
2308
2309   // Verify that the packet re-serializes identically.
2310   QuicFrames frames;
2311   frames.push_back(QuicFrame(frame));
2312   scoped_ptr<QuicPacket> data(
2313       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
2314   ASSERT_TRUE(data != NULL);
2315
2316   test::CompareCharArraysWithHexError("constructed packet",
2317                                       data->data(), data->length(),
2318                                       AsChars(packet), arraysize(packet));
2319 }
2320
2321 TEST_P(QuicFramerTest, AckFrame500Nacks) {
2322   if (framer_.version() <= QUIC_VERSION_15) {
2323     return;
2324   }
2325   unsigned char packet[] = {
2326     // public flags (8 byte connection_id)
2327     0x3C,
2328     // connection_id
2329     0x10, 0x32, 0x54, 0x76,
2330     0x98, 0xBA, 0xDC, 0xFE,
2331     // packet sequence number
2332     0xA8, 0x9A, 0x78, 0x56,
2333     0x34, 0x12,
2334     // private flags (entropy)
2335     0x01,
2336
2337     // frame type (ack frame)
2338     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2339     0x6C,
2340     // entropy hash of all received packets.
2341     0xBA,
2342     // largest observed packet sequence number
2343     0xBF, 0x9A, 0x78, 0x56,
2344     0x34, 0x12,
2345     // Zero delta time.
2346     0x0, 0x0,
2347     // num missing packet ranges
2348     0x02,
2349     // missing packet delta
2350     0x01,
2351     // 243 more missing packets in range.
2352     // The ranges are listed in this order so the re-constructed packet matches.
2353     0xF3,
2354     // No gap between ranges
2355     0x00,
2356     // 255 more missing packets in range.
2357     0xFF,
2358     // No revived packets.
2359     0x00,
2360   };
2361
2362   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2363   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2364
2365   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2366   ASSERT_TRUE(visitor_.header_.get());
2367   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2368
2369   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2370   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2371   QuicAckFrame* frame = visitor_.ack_frames_[0];
2372   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
2373   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
2374             frame->received_info.largest_observed);
2375   EXPECT_EQ(0u, frame->received_info.revived_packets.size());
2376   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
2377   SequenceNumberSet::const_iterator first_missing_iter =
2378       frame->received_info.missing_packets.begin();
2379   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
2380   SequenceNumberSet::const_reverse_iterator last_missing_iter =
2381       frame->received_info.missing_packets.rbegin();
2382   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
2383
2384   // Verify that the packet re-serializes identically.
2385   QuicFrames frames;
2386   frames.push_back(QuicFrame(frame));
2387   scoped_ptr<QuicPacket> data(
2388       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
2389   ASSERT_TRUE(data != NULL);
2390
2391   test::CompareCharArraysWithHexError("constructed packet",
2392                                       data->data(), data->length(),
2393                                       AsChars(packet), arraysize(packet));
2394 }
2395
2396 TEST_P(QuicFramerTest, AckFrame500Nacks15) {
2397   if (framer_.version() != QUIC_VERSION_15) {
2398     return;
2399   }
2400   unsigned char packet[] = {
2401     // public flags (8 byte connection_id)
2402     0x3C,
2403     // connection_id
2404     0x10, 0x32, 0x54, 0x76,
2405     0x98, 0xBA, 0xDC, 0xFE,
2406     // packet sequence number
2407     0xA8, 0x9A, 0x78, 0x56,
2408     0x34, 0x12,
2409     // private flags (entropy)
2410     0x01,
2411
2412     // frame type (ack frame)
2413     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2414     0x6C,
2415     // entropy hash of sent packets till least awaiting - 1.
2416     0xAB,
2417     // least packet sequence number awaiting an ack, delta from sequence number.
2418     0x08, 0x00, 0x00, 0x00,
2419     0x00, 0x00,
2420     // entropy hash of all received packets.
2421     0xBA,
2422     // largest observed packet sequence number
2423     0xBF, 0x9A, 0x78, 0x56,
2424     0x34, 0x12,
2425     // Zero delta time.
2426     0x0, 0x0,
2427     // num missing packet ranges
2428     0x02,
2429     // missing packet delta
2430     0x01,
2431     // 243 more missing packets in range.
2432     // The ranges are listed in this order so the re-constructed packet matches.
2433     0xF3,
2434     // No gap between ranges
2435     0x00,
2436     // 255 more missing packets in range.
2437     0xFF,
2438     // No revived packets.
2439     0x00,
2440   };
2441
2442   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2443   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2444
2445   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2446   ASSERT_TRUE(visitor_.header_.get());
2447   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2448
2449   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2450   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2451   QuicAckFrame* frame = visitor_.ack_frames_[0];
2452   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
2453   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
2454   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
2455             frame->received_info.largest_observed);
2456   EXPECT_EQ(0u, frame->received_info.revived_packets.size());
2457   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
2458   SequenceNumberSet::const_iterator first_missing_iter =
2459       frame->received_info.missing_packets.begin();
2460   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
2461   SequenceNumberSet::const_reverse_iterator last_missing_iter =
2462       frame->received_info.missing_packets.rbegin();
2463   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
2464   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
2465
2466   // Verify that the packet re-serializes identically.
2467   QuicFrames frames;
2468   frames.push_back(QuicFrame(frame));
2469   scoped_ptr<QuicPacket> data(
2470       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
2471   ASSERT_TRUE(data != NULL);
2472
2473   test::CompareCharArraysWithHexError("constructed packet",
2474                                       data->data(), data->length(),
2475                                       AsChars(packet), arraysize(packet));
2476 }
2477
2478 TEST_P(QuicFramerTest, AckFrame500NacksV13) {
2479   if (framer_.version() != QUIC_VERSION_13) {
2480     return;
2481   }
2482   unsigned char packet[] = {
2483     // public flags (8 byte connection_id)
2484     0x3C,
2485     // connection_id
2486     0x10, 0x32, 0x54, 0x76,
2487     0x98, 0xBA, 0xDC, 0xFE,
2488     // packet sequence number
2489     0xA8, 0x9A, 0x78, 0x56,
2490     0x34, 0x12,
2491     // private flags (entropy)
2492     0x01,
2493
2494     // frame type (ack frame)
2495     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2496     0x6C,
2497     // entropy hash of sent packets till least awaiting - 1.
2498     0xAB,
2499     // least packet sequence number awaiting an ack, delta from sequence number.
2500     0x08, 0x00, 0x00, 0x00,
2501     0x00, 0x00,
2502     // entropy hash of all received packets.
2503     0xBA,
2504     // largest observed packet sequence number
2505     0xBF, 0x9A, 0x78, 0x56,
2506     0x34, 0x12,
2507     // Zero delta time.
2508     0x0, 0x0,
2509     // num missing packet ranges
2510     0x02,
2511     // missing packet delta
2512     0x01,
2513     // 243 more missing packets in range.
2514     // The ranges are listed in this order so the re-constructed packet matches.
2515     0xF3,
2516     // No gap between ranges
2517     0x00,
2518     // 255 more missing packets in range.
2519     0xFF,
2520   };
2521
2522   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2523   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2524
2525   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2526   ASSERT_TRUE(visitor_.header_.get());
2527   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2528
2529   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2530   ASSERT_EQ(1u, visitor_.ack_frames_.size());
2531   QuicAckFrame* frame = visitor_.ack_frames_[0];
2532   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
2533   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
2534   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
2535             frame->received_info.largest_observed);
2536   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
2537   SequenceNumberSet::const_iterator first_missing_iter =
2538       frame->received_info.missing_packets.begin();
2539   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
2540   SequenceNumberSet::const_reverse_iterator last_missing_iter =
2541       frame->received_info.missing_packets.rbegin();
2542   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
2543   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
2544
2545   // Verify that the packet re-serializes identically.
2546   QuicFrames frames;
2547   frames.push_back(QuicFrame(frame));
2548   scoped_ptr<QuicPacket> data(
2549       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
2550   ASSERT_TRUE(data != NULL);
2551
2552   test::CompareCharArraysWithHexError("constructed packet",
2553                                       data->data(), data->length(),
2554                                       AsChars(packet), arraysize(packet));
2555 }
2556
2557 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCP) {
2558   if (framer_.version() == QUIC_VERSION_13) {
2559     return;
2560   }
2561   unsigned char packet[] = {
2562     // public flags (8 byte connection_id)
2563     0x3C,
2564     // connection_id
2565     0x10, 0x32, 0x54, 0x76,
2566     0x98, 0xBA, 0xDC, 0xFE,
2567     // packet sequence number
2568     0xBC, 0x9A, 0x78, 0x56,
2569     0x34, 0x12,
2570     // private flags
2571     0x00,
2572
2573     // frame type (congestion feedback frame)
2574     0x20,
2575     // congestion feedback type (tcp)
2576     0x00,
2577     // ack_frame.feedback.tcp.receive_window
2578     0x03, 0x04,
2579   };
2580
2581   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2582   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2583
2584   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2585   ASSERT_TRUE(visitor_.header_.get());
2586   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2587
2588   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2589   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2590   const QuicCongestionFeedbackFrame& frame =
2591       *visitor_.congestion_feedback_frames_[0];
2592   ASSERT_EQ(kTCP, frame.type);
2593   EXPECT_EQ(0x4030u, frame.tcp.receive_window);
2594
2595   // Now test framing boundaries
2596   for (size_t i = kQuicFrameTypeSize; i < 4; ++i) {
2597     string expected_error;
2598     if (i < 2) {
2599       expected_error = "Unable to read congestion feedback type.";
2600     } else if (i < 4) {
2601       expected_error = "Unable to read receive window.";
2602     }
2603     CheckProcessingFails(
2604         packet,
2605         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2606                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2607         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2608   }
2609 }
2610
2611 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCPV13) {
2612   if (framer_.version() != QUIC_VERSION_13) {
2613     return;
2614   }
2615   unsigned char packet[] = {
2616     // public flags (8 byte connection_id)
2617     0x3C,
2618     // connection_id
2619     0x10, 0x32, 0x54, 0x76,
2620     0x98, 0xBA, 0xDC, 0xFE,
2621     // packet sequence number
2622     0xBC, 0x9A, 0x78, 0x56,
2623     0x34, 0x12,
2624     // private flags
2625     0x00,
2626
2627     // frame type (congestion feedback frame)
2628     0x20,
2629     // congestion feedback type (tcp)
2630     0x00,
2631     // ack_frame.feedback.tcp.accumulated_number_of_lost_packets
2632     0x01, 0x02,
2633     // ack_frame.feedback.tcp.receive_window
2634     0x03, 0x04,
2635   };
2636
2637   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2638   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2639
2640   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2641   ASSERT_TRUE(visitor_.header_.get());
2642   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2643
2644   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2645   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2646   const QuicCongestionFeedbackFrame& frame =
2647       *visitor_.congestion_feedback_frames_[0];
2648   ASSERT_EQ(kTCP, frame.type);
2649   EXPECT_EQ(0x4030u, frame.tcp.receive_window);
2650
2651   // Now test framing boundaries
2652   for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
2653     string expected_error;
2654     if (i < 2) {
2655       expected_error = "Unable to read congestion feedback type.";
2656     } else if (i < 4) {
2657       expected_error = "Unable to read accumulated number of lost packets.";
2658     } else if (i < 6) {
2659       expected_error = "Unable to read receive window.";
2660     }
2661     CheckProcessingFails(
2662         packet,
2663         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2664                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2665         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2666   }
2667 }
2668
2669 TEST_P(QuicFramerTest, CongestionFeedbackFrameInterArrival) {
2670   if (framer_.version() == QUIC_VERSION_13) {
2671     return;
2672   }
2673   unsigned char packet[] = {
2674     // public flags (8 byte connection_id)
2675     0x3C,
2676     // connection_id
2677     0x10, 0x32, 0x54, 0x76,
2678     0x98, 0xBA, 0xDC, 0xFE,
2679     // packet sequence number
2680     0xBC, 0x9A, 0x78, 0x56,
2681     0x34, 0x12,
2682     // private flags
2683     0x00,
2684
2685     // frame type (congestion feedback frame)
2686     0x20,
2687     // congestion feedback type (inter arrival)
2688     0x01,
2689     // num received packets
2690     0x03,
2691     // lowest sequence number
2692     0xBA, 0x9A, 0x78, 0x56,
2693     0x34, 0x12,
2694     // receive time
2695     0x87, 0x96, 0xA5, 0xB4,
2696     0xC3, 0xD2, 0xE1, 0x07,
2697     // sequence delta
2698     0x01, 0x00,
2699     // time delta
2700     0x01, 0x00, 0x00, 0x00,
2701     // sequence delta (skip one packet)
2702     0x03, 0x00,
2703     // time delta
2704     0x02, 0x00, 0x00, 0x00,
2705   };
2706
2707   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2708   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2709
2710   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2711   ASSERT_TRUE(visitor_.header_.get());
2712   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2713
2714   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2715   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2716   const QuicCongestionFeedbackFrame& frame =
2717       *visitor_.congestion_feedback_frames_[0];
2718   ASSERT_EQ(kInterArrival, frame.type);
2719   ASSERT_EQ(3u, frame.inter_arrival.received_packet_times.size());
2720   TimeMap::const_iterator iter =
2721       frame.inter_arrival.received_packet_times.begin();
2722   EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter->first);
2723   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
2724             iter->second.Subtract(start_).ToMicroseconds());
2725   ++iter;
2726   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter->first);
2727   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
2728             iter->second.Subtract(start_).ToMicroseconds());
2729   ++iter;
2730   EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter->first);
2731   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
2732             iter->second.Subtract(start_).ToMicroseconds());
2733
2734   // Now test framing boundaries
2735   for (size_t i = kQuicFrameTypeSize; i < 29; ++i) {
2736     string expected_error;
2737     if (i < 2) {
2738       expected_error = "Unable to read congestion feedback type.";
2739     } else if (i < 3) {
2740       expected_error = "Unable to read num received packets.";
2741     } else if (i < 9) {
2742       expected_error = "Unable to read smallest received.";
2743     } else if (i < 17) {
2744       expected_error = "Unable to read time received.";
2745     } else if (i < 19) {
2746       expected_error = "Unable to read sequence delta in received packets.";
2747     } else if (i < 23) {
2748       expected_error = "Unable to read time delta in received packets.";
2749     } else if (i < 25) {
2750       expected_error = "Unable to read sequence delta in received packets.";
2751     } else if (i < 29) {
2752       expected_error = "Unable to read time delta in received packets.";
2753     }
2754     CheckProcessingFails(
2755         packet,
2756         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2757                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2758         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2759   }
2760 }
2761
2762 TEST_P(QuicFramerTest, CongestionFeedbackFrameInterArrivalV13) {
2763   if (framer_.version() != QUIC_VERSION_13) {
2764     return;
2765   }
2766   unsigned char packet[] = {
2767     // public flags (8 byte connection_id)
2768     0x3C,
2769     // connection_id
2770     0x10, 0x32, 0x54, 0x76,
2771     0x98, 0xBA, 0xDC, 0xFE,
2772     // packet sequence number
2773     0xBC, 0x9A, 0x78, 0x56,
2774     0x34, 0x12,
2775     // private flags
2776     0x00,
2777
2778     // frame type (congestion feedback frame)
2779     0x20,
2780     // congestion feedback type (inter arrival)
2781     0x01,
2782     // accumulated_number_of_lost_packets
2783     0x02, 0x03,
2784     // num received packets
2785     0x03,
2786     // lowest sequence number
2787     0xBA, 0x9A, 0x78, 0x56,
2788     0x34, 0x12,
2789     // receive time
2790     0x87, 0x96, 0xA5, 0xB4,
2791     0xC3, 0xD2, 0xE1, 0x07,
2792     // sequence delta
2793     0x01, 0x00,
2794     // time delta
2795     0x01, 0x00, 0x00, 0x00,
2796     // sequence delta (skip one packet)
2797     0x03, 0x00,
2798     // time delta
2799     0x02, 0x00, 0x00, 0x00,
2800   };
2801
2802   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2803   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2804
2805   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2806   ASSERT_TRUE(visitor_.header_.get());
2807   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2808
2809   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2810   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2811   const QuicCongestionFeedbackFrame& frame =
2812       *visitor_.congestion_feedback_frames_[0];
2813   ASSERT_EQ(kInterArrival, frame.type);
2814   ASSERT_EQ(3u, frame.inter_arrival.received_packet_times.size());
2815   TimeMap::const_iterator iter =
2816       frame.inter_arrival.received_packet_times.begin();
2817   EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter->first);
2818   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
2819             iter->second.Subtract(start_).ToMicroseconds());
2820   ++iter;
2821   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter->first);
2822   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
2823             iter->second.Subtract(start_).ToMicroseconds());
2824   ++iter;
2825   EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter->first);
2826   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
2827             iter->second.Subtract(start_).ToMicroseconds());
2828
2829   // Now test framing boundaries
2830   for (size_t i = kQuicFrameTypeSize; i < 31; ++i) {
2831     string expected_error;
2832     if (i < 2) {
2833       expected_error = "Unable to read congestion feedback type.";
2834     } else if (i < 4) {
2835       expected_error = "Unable to read accumulated number of lost packets.";
2836     } else if (i < 5) {
2837       expected_error = "Unable to read num received packets.";
2838     } else if (i < 11) {
2839       expected_error = "Unable to read smallest received.";
2840     } else if (i < 19) {
2841       expected_error = "Unable to read time received.";
2842     } else if (i < 21) {
2843       expected_error = "Unable to read sequence delta in received packets.";
2844     } else if (i < 25) {
2845       expected_error = "Unable to read time delta in received packets.";
2846     } else if (i < 27) {
2847       expected_error = "Unable to read sequence delta in received packets.";
2848     } else if (i < 31) {
2849       expected_error = "Unable to read time delta in received packets.";
2850     }
2851     CheckProcessingFails(
2852         packet,
2853         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2854                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2855         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2856   }
2857 }
2858
2859 TEST_P(QuicFramerTest, CongestionFeedbackFrameFixRate) {
2860   unsigned char packet[] = {
2861     // public flags (8 byte connection_id)
2862     0x3C,
2863     // connection_id
2864     0x10, 0x32, 0x54, 0x76,
2865     0x98, 0xBA, 0xDC, 0xFE,
2866     // packet sequence number
2867     0xBC, 0x9A, 0x78, 0x56,
2868     0x34, 0x12,
2869     // private flags
2870     0x00,
2871
2872     // frame type (congestion feedback frame)
2873     0x20,
2874     // congestion feedback type (fix rate)
2875     0x02,
2876     // bitrate_in_bytes_per_second;
2877     0x01, 0x02, 0x03, 0x04,
2878   };
2879
2880   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2881   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2882
2883   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2884   ASSERT_TRUE(visitor_.header_.get());
2885   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2886
2887   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2888   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2889   const QuicCongestionFeedbackFrame& frame =
2890       *visitor_.congestion_feedback_frames_[0];
2891   ASSERT_EQ(kFixRate, frame.type);
2892   EXPECT_EQ(static_cast<uint32>(0x04030201),
2893             frame.fix_rate.bitrate.ToBytesPerSecond());
2894
2895   // Now test framing boundaries
2896   for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
2897     string expected_error;
2898     if (i < 2) {
2899       expected_error = "Unable to read congestion feedback type.";
2900     } else if (i < 6) {
2901       expected_error = "Unable to read bitrate.";
2902     }
2903     CheckProcessingFails(
2904         packet,
2905         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2906                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2907         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2908   }
2909 }
2910
2911 TEST_P(QuicFramerTest, CongestionFeedbackFrameInvalidFeedback) {
2912   unsigned char packet[] = {
2913     // public flags (8 byte connection_id)
2914     0x3C,
2915     // connection_id
2916     0x10, 0x32, 0x54, 0x76,
2917     0x98, 0xBA, 0xDC, 0xFE,
2918     // packet sequence number
2919     0xBC, 0x9A, 0x78, 0x56,
2920     0x34, 0x12,
2921     // private flags
2922     0x00,
2923
2924     // frame type (congestion feedback frame)
2925     0x20,
2926     // congestion feedback type (invalid)
2927     0x03,
2928   };
2929
2930   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2931   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
2932   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2933   EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA, framer_.error());
2934 }
2935
2936 TEST_P(QuicFramerTest, StopWaitingFrame) {
2937   if (framer_.version() <= QUIC_VERSION_15) {
2938     return;
2939   }
2940   unsigned char packet[] = {
2941     // public flags (8 byte connection_id)
2942     0x3C,
2943     // connection_id
2944     0x10, 0x32, 0x54, 0x76,
2945     0x98, 0xBA, 0xDC, 0xFE,
2946     // packet sequence number
2947     0xA8, 0x9A, 0x78, 0x56,
2948     0x34, 0x12,
2949     // private flags (entropy)
2950     0x01,
2951
2952     // frame type (ack frame)
2953     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2954     0x06,
2955     // entropy hash of sent packets till least awaiting - 1.
2956     0xAB,
2957     // least packet sequence number awaiting an ack, delta from sequence number.
2958     0x08, 0x00, 0x00, 0x00,
2959     0x00, 0x00,
2960   };
2961
2962   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2963   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2964
2965   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2966   ASSERT_TRUE(visitor_.header_.get());
2967   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2968
2969   EXPECT_EQ(0u, visitor_.stream_frames_.size());
2970   ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
2971   const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
2972   EXPECT_EQ(0xAB, frame.entropy_hash);
2973   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.least_unacked);
2974
2975   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
2976   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
2977   const size_t frame_size = 7;
2978   for (size_t i = kQuicFrameTypeSize; i < frame_size; ++i) {
2979     string expected_error;
2980     if (i < kLeastUnackedOffset) {
2981       expected_error = "Unable to read entropy hash for sent packets.";
2982     } else {
2983       expected_error = "Unable to read least unacked delta.";
2984     }
2985     CheckProcessingFails(
2986         packet,
2987         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2988                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2989         expected_error, QUIC_INVALID_STOP_WAITING_DATA);
2990   }
2991 }
2992
2993 TEST_P(QuicFramerTest, RstStreamFrameVersion13) {
2994   if (version_ > QUIC_VERSION_13) {
2995     return;
2996   }
2997
2998   unsigned char packet[] = {
2999     // public flags (8 byte connection_id)
3000     0x3C,
3001     // connection_id
3002     0x10, 0x32, 0x54, 0x76,
3003     0x98, 0xBA, 0xDC, 0xFE,
3004     // packet sequence number
3005     0xBC, 0x9A, 0x78, 0x56,
3006     0x34, 0x12,
3007     // private flags
3008     0x00,
3009
3010     // frame type (rst stream frame)
3011     0x01,
3012     // stream id
3013     0x04, 0x03, 0x02, 0x01,
3014     // error code
3015     0x01, 0x00, 0x00, 0x00,
3016
3017     // error details length
3018     0x0d, 0x00,
3019     // error details
3020     'b',  'e',  'c',  'a',
3021     'u',  's',  'e',  ' ',
3022     'I',  ' ',  'c',  'a',
3023     'n',
3024   };
3025
3026   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3027   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3028
3029   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3030   ASSERT_TRUE(visitor_.header_.get());
3031   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3032
3033   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
3034   EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
3035   EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
3036
3037   // Now test framing boundaries
3038   for (size_t i = kQuicFrameTypeSize;
3039        i < QuicFramer::GetMinRstStreamFrameSize(version_); ++i) {
3040     string expected_error;
3041     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
3042       expected_error = "Unable to read stream_id.";
3043     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
3044                kQuicErrorCodeSize) {
3045       expected_error = "Unable to read rst stream error code.";
3046     } else {
3047       expected_error = "Unable to read rst stream error details.";
3048     }
3049     CheckProcessingFails(
3050         packet,
3051         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3052                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3053         expected_error, QUIC_INVALID_RST_STREAM_DATA);
3054   }
3055 }
3056
3057 TEST_P(QuicFramerTest, RstStreamFrameQuic) {
3058   if (version_ <= QUIC_VERSION_13) {
3059     return;
3060   }
3061
3062   unsigned char packet[] = {
3063     // public flags (8 byte connection_id)
3064     0x3C,
3065     // connection_id
3066     0x10, 0x32, 0x54, 0x76,
3067     0x98, 0xBA, 0xDC, 0xFE,
3068     // packet sequence number
3069     0xBC, 0x9A, 0x78, 0x56,
3070     0x34, 0x12,
3071     // private flags
3072     0x00,
3073
3074     // frame type (rst stream frame)
3075     0x01,
3076     // stream id
3077     0x04, 0x03, 0x02, 0x01,
3078
3079     // sent byte offset
3080     0x01, 0x02, 0x03, 0x04,
3081     0x05, 0x06, 0x07, 0x08,
3082
3083     // error code
3084     0x01, 0x00, 0x00, 0x00,
3085
3086     // error details length
3087     0x0d, 0x00,
3088     // error details
3089     'b',  'e',  'c',  'a',
3090     'u',  's',  'e',  ' ',
3091     'I',  ' ',  'c',  'a',
3092     'n',
3093   };
3094
3095   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3096   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3097
3098   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3099   ASSERT_TRUE(visitor_.header_.get());
3100   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3101
3102   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
3103   EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
3104   EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
3105   EXPECT_EQ(GG_UINT64_C(0x0807060504030201),
3106             visitor_.rst_stream_frame_.byte_offset);
3107
3108   // Now test framing boundaries
3109   for (size_t i = kQuicFrameTypeSize;
3110        i < QuicFramer::GetMinRstStreamFrameSize(version_); ++i) {
3111     string expected_error;
3112     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
3113       expected_error = "Unable to read stream_id.";
3114     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
3115                        + kQuicMaxStreamOffsetSize) {
3116       expected_error = "Unable to read rst stream sent byte offset.";
3117     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
3118                        + kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
3119       expected_error = "Unable to read rst stream error code.";
3120     } else {
3121       expected_error = "Unable to read rst stream error details.";
3122     }
3123     CheckProcessingFails(
3124         packet,
3125         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3126                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3127         expected_error, QUIC_INVALID_RST_STREAM_DATA);
3128   }
3129 }
3130
3131 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
3132   unsigned char packet[] = {
3133     // public flags (8 byte connection_id)
3134     0x3C,
3135     // connection_id
3136     0x10, 0x32, 0x54, 0x76,
3137     0x98, 0xBA, 0xDC, 0xFE,
3138     // packet sequence number
3139     0xBC, 0x9A, 0x78, 0x56,
3140     0x34, 0x12,
3141     // private flags
3142     0x00,
3143
3144     // frame type (connection close frame)
3145     0x02,
3146     // error code
3147     0x11, 0x00, 0x00, 0x00,
3148
3149     // error details length
3150     0x0d, 0x00,
3151     // error details
3152     'b',  'e',  'c',  'a',
3153     'u',  's',  'e',  ' ',
3154     'I',  ' ',  'c',  'a',
3155     'n',
3156   };
3157
3158   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3159   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3160
3161   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3162   ASSERT_TRUE(visitor_.header_.get());
3163   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3164
3165   EXPECT_EQ(0u, visitor_.stream_frames_.size());
3166
3167   EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
3168   EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
3169
3170   ASSERT_EQ(0u, visitor_.ack_frames_.size());
3171
3172   // Now test framing boundaries
3173   for (size_t i = kQuicFrameTypeSize;
3174        i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
3175     string expected_error;
3176     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
3177       expected_error = "Unable to read connection close error code.";
3178     } else {
3179       expected_error = "Unable to read connection close error details.";
3180     }
3181     CheckProcessingFails(
3182         packet,
3183         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3184                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3185         expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
3186   }
3187 }
3188
3189 TEST_P(QuicFramerTest, GoAwayFrame) {
3190   unsigned char packet[] = {
3191     // public flags (8 byte connection_id)
3192     0x3C,
3193     // connection_id
3194     0x10, 0x32, 0x54, 0x76,
3195     0x98, 0xBA, 0xDC, 0xFE,
3196     // packet sequence number
3197     0xBC, 0x9A, 0x78, 0x56,
3198     0x34, 0x12,
3199     // private flags
3200     0x00,
3201
3202     // frame type (go away frame)
3203     0x03,
3204     // error code
3205     0x09, 0x00, 0x00, 0x00,
3206     // stream id
3207     0x04, 0x03, 0x02, 0x01,
3208     // error details length
3209     0x0d, 0x00,
3210     // error details
3211     'b',  'e',  'c',  'a',
3212     'u',  's',  'e',  ' ',
3213     'I',  ' ',  'c',  'a',
3214     'n',
3215   };
3216
3217   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3218   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3219
3220   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3221   ASSERT_TRUE(visitor_.header_.get());
3222   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3223
3224   EXPECT_EQ(GG_UINT64_C(0x01020304),
3225             visitor_.goaway_frame_.last_good_stream_id);
3226   EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
3227   EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
3228
3229   const size_t reason_size = arraysize("because I can") - 1;
3230   // Now test framing boundaries
3231   for (size_t i = kQuicFrameTypeSize;
3232        i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
3233     string expected_error;
3234     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
3235       expected_error = "Unable to read go away error code.";
3236     } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
3237                kQuicMaxStreamIdSize) {
3238       expected_error = "Unable to read last good stream id.";
3239     } else {
3240       expected_error = "Unable to read goaway reason.";
3241     }
3242     CheckProcessingFails(
3243         packet,
3244         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3245                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3246         expected_error, QUIC_INVALID_GOAWAY_DATA);
3247   }
3248 }
3249
3250 TEST_P(QuicFramerTest, WindowUpdateFrame) {
3251   unsigned char packet[] = {
3252     // public flags (8 byte connection_id)
3253     0x3C,
3254     // connection_id
3255     0x10, 0x32, 0x54, 0x76,
3256     0x98, 0xBA, 0xDC, 0xFE,
3257     // packet sequence number
3258     0xBC, 0x9A, 0x78, 0x56,
3259     0x34, 0x12,
3260     // private flags
3261     0x00,
3262
3263     // frame type (window update frame)
3264     0x04,
3265     // stream id
3266     0x04, 0x03, 0x02, 0x01,
3267     // byte offset
3268     0x05, 0x06, 0x07, 0x08,
3269     0x09, 0x0a, 0x0b, 0x0c,
3270   };
3271
3272   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3273
3274   // WINDOW_UPDATE frame introduced in QUIC_VERSION_14.
3275   if (version_ <= QUIC_VERSION_13) {
3276     string expected_error = "Trying to read a WindowUpdateFrame in " +
3277                             QuicVersionToString(version_);
3278     EXPECT_DFATAL(framer_.ProcessPacket(encrypted), expected_error);
3279     return;
3280   }
3281
3282   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3283
3284   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3285   ASSERT_TRUE(visitor_.header_.get());
3286   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3287
3288   EXPECT_EQ(GG_UINT64_C(0x01020304),
3289             visitor_.window_update_frame_.stream_id);
3290   EXPECT_EQ(GG_UINT64_C(0x0c0b0a0908070605),
3291             visitor_.window_update_frame_.byte_offset);
3292
3293   // Now test framing boundaries
3294   for (size_t i = kQuicFrameTypeSize;
3295        i < QuicFramer::GetWindowUpdateFrameSize(); ++i) {
3296     string expected_error;
3297     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
3298       expected_error = "Unable to read stream_id.";
3299     } else {
3300       expected_error = "Unable to read window byte_offset.";
3301     }
3302     CheckProcessingFails(
3303         packet,
3304         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3305                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3306         expected_error, QUIC_INVALID_WINDOW_UPDATE_DATA);
3307   }
3308 }
3309
3310 TEST_P(QuicFramerTest, BlockedFrame) {
3311   unsigned char packet[] = {
3312     // public flags (8 byte connection_id)
3313     0x3C,
3314     // connection_id
3315     0x10, 0x32, 0x54, 0x76,
3316     0x98, 0xBA, 0xDC, 0xFE,
3317     // packet sequence number
3318     0xBC, 0x9A, 0x78, 0x56,
3319     0x34, 0x12,
3320     // private flags
3321     0x00,
3322
3323     // frame type (blocked frame)
3324     0x05,
3325     // stream id
3326     0x04, 0x03, 0x02, 0x01,
3327   };
3328
3329   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3330
3331   // BLOCKED frame introduced in QUIC_VERSION_14.
3332   if (version_ <= QUIC_VERSION_13) {
3333     string expected_error =
3334         "Trying to read a BlockedFrame in " + QuicVersionToString(version_);
3335     EXPECT_DFATAL(framer_.ProcessPacket(encrypted), expected_error);
3336     return;
3337   }
3338
3339   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3340
3341   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3342   ASSERT_TRUE(visitor_.header_.get());
3343   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3344
3345   EXPECT_EQ(GG_UINT64_C(0x01020304),
3346             visitor_.blocked_frame_.stream_id);
3347
3348   // Now test framing boundaries
3349   for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetBlockedFrameSize();
3350        ++i) {
3351     string expected_error = "Unable to read stream_id.";
3352     CheckProcessingFails(
3353         packet,
3354         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3355                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
3356         expected_error, QUIC_INVALID_BLOCKED_DATA);
3357   }
3358 }
3359
3360 TEST_P(QuicFramerTest, PublicResetPacket) {
3361   unsigned char packet[] = {
3362     // public flags (public reset, 8 byte connection_id)
3363     0x0E,
3364     // connection_id
3365     0x10, 0x32, 0x54, 0x76,
3366     0x98, 0xBA, 0xDC, 0xFE,
3367     // message tag (kPRST)
3368     'P', 'R', 'S', 'T',
3369     // num_entries (2) + padding
3370     0x02, 0x00, 0x00, 0x00,
3371     // tag kRNON
3372     'R', 'N', 'O', 'N',
3373     // end offset 8
3374     0x08, 0x00, 0x00, 0x00,
3375     // tag kRSEQ
3376     'R', 'S', 'E', 'Q',
3377     // end offset 16
3378     0x10, 0x00, 0x00, 0x00,
3379     // nonce proof
3380     0x89, 0x67, 0x45, 0x23,
3381     0x01, 0xEF, 0xCD, 0xAB,
3382     // rejected sequence number
3383     0xBC, 0x9A, 0x78, 0x56,
3384     0x34, 0x12, 0x00, 0x00,
3385   };
3386
3387   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3388   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3389   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
3390   ASSERT_TRUE(visitor_.public_reset_packet_.get());
3391   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
3392             visitor_.public_reset_packet_->public_header.connection_id);
3393   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
3394   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
3395   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
3396             visitor_.public_reset_packet_->nonce_proof);
3397   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
3398             visitor_.public_reset_packet_->rejected_sequence_number);
3399   EXPECT_TRUE(
3400       visitor_.public_reset_packet_->client_address.address().empty());
3401
3402   // Now test framing boundaries
3403   for (size_t i = 0; i < arraysize(packet); ++i) {
3404     string expected_error;
3405     DVLOG(1) << "iteration: " << i;
3406     if (i < kConnectionIdOffset) {
3407       expected_error = "Unable to read public flags.";
3408       CheckProcessingFails(packet, i, expected_error,
3409                            QUIC_INVALID_PACKET_HEADER);
3410     } else if (i < kPublicResetPacketMessageTagOffset) {
3411       expected_error = "Unable to read ConnectionId.";
3412       CheckProcessingFails(packet, i, expected_error,
3413                            QUIC_INVALID_PACKET_HEADER);
3414     } else {
3415       expected_error = "Unable to read reset message.";
3416       CheckProcessingFails(packet, i, expected_error,
3417                            QUIC_INVALID_PUBLIC_RST_PACKET);
3418     }
3419   }
3420 }
3421
3422 TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
3423   unsigned char packet[] = {
3424     // public flags (public reset, 8 byte connection_id)
3425     0x0E,
3426     // connection_id
3427     0x10, 0x32, 0x54, 0x76,
3428     0x98, 0xBA, 0xDC, 0xFE,
3429     // message tag (kPRST)
3430     'P', 'R', 'S', 'T',
3431     // num_entries (2) + padding
3432     0x02, 0x00, 0x00, 0x00,
3433     // tag kRNON
3434     'R', 'N', 'O', 'N',
3435     // end offset 8
3436     0x08, 0x00, 0x00, 0x00,
3437     // tag kRSEQ
3438     'R', 'S', 'E', 'Q',
3439     // end offset 16
3440     0x10, 0x00, 0x00, 0x00,
3441     // nonce proof
3442     0x89, 0x67, 0x45, 0x23,
3443     0x01, 0xEF, 0xCD, 0xAB,
3444     // rejected sequence number
3445     0xBC, 0x9A, 0x78, 0x56,
3446     0x34, 0x12, 0x00, 0x00,
3447     // trailing junk
3448     'j', 'u', 'n', 'k',
3449   };
3450
3451   string expected_error = "Unable to read reset message.";
3452   CheckProcessingFails(packet, arraysize(packet), expected_error,
3453                        QUIC_INVALID_PUBLIC_RST_PACKET);
3454 }
3455
3456 TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
3457   unsigned char packet[] = {
3458     // public flags (public reset, 8 byte connection_id)
3459     0x0E,
3460     // connection_id
3461     0x10, 0x32, 0x54, 0x76,
3462     0x98, 0xBA, 0xDC, 0xFE,
3463     // message tag (kPRST)
3464     'P', 'R', 'S', 'T',
3465     // num_entries (3) + padding
3466     0x03, 0x00, 0x00, 0x00,
3467     // tag kRNON
3468     'R', 'N', 'O', 'N',
3469     // end offset 8
3470     0x08, 0x00, 0x00, 0x00,
3471     // tag kRSEQ
3472     'R', 'S', 'E', 'Q',
3473     // end offset 16
3474     0x10, 0x00, 0x00, 0x00,
3475     // tag kCADR
3476     'C', 'A', 'D', 'R',
3477     // end offset 24
3478     0x18, 0x00, 0x00, 0x00,
3479     // nonce proof
3480     0x89, 0x67, 0x45, 0x23,
3481     0x01, 0xEF, 0xCD, 0xAB,
3482     // rejected sequence number
3483     0xBC, 0x9A, 0x78, 0x56,
3484     0x34, 0x12, 0x00, 0x00,
3485     // client address: 4.31.198.44:443
3486     0x02, 0x00,
3487     0x04, 0x1F, 0xC6, 0x2C,
3488     0xBB, 0x01,
3489   };
3490
3491   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3492   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3493   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
3494   ASSERT_TRUE(visitor_.public_reset_packet_.get());
3495   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
3496             visitor_.public_reset_packet_->public_header.connection_id);
3497   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
3498   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
3499   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
3500             visitor_.public_reset_packet_->nonce_proof);
3501   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
3502             visitor_.public_reset_packet_->rejected_sequence_number);
3503   EXPECT_EQ("4.31.198.44",
3504             IPAddressToString(visitor_.public_reset_packet_->
3505                 client_address.address()));
3506   EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
3507
3508   // Now test framing boundaries
3509   for (size_t i = 0; i < arraysize(packet); ++i) {
3510     string expected_error;
3511     DVLOG(1) << "iteration: " << i;
3512     if (i < kConnectionIdOffset) {
3513       expected_error = "Unable to read public flags.";
3514       CheckProcessingFails(packet, i, expected_error,
3515                            QUIC_INVALID_PACKET_HEADER);
3516     } else if (i < kPublicResetPacketMessageTagOffset) {
3517       expected_error = "Unable to read ConnectionId.";
3518       CheckProcessingFails(packet, i, expected_error,
3519                            QUIC_INVALID_PACKET_HEADER);
3520     } else {
3521       expected_error = "Unable to read reset message.";
3522       CheckProcessingFails(packet, i, expected_error,
3523                            QUIC_INVALID_PUBLIC_RST_PACKET);
3524     }
3525   }
3526 }
3527
3528 // TODO(wtc): remove this test when we drop support for QUIC_VERSION_13.
3529 TEST_P(QuicFramerTest, PublicResetPacketOld) {
3530   unsigned char packet[] = {
3531     // public flags (public reset, 8 byte connection_id)
3532     0x3E,
3533     // connection_id
3534     0x10, 0x32, 0x54, 0x76,
3535     0x98, 0xBA, 0xDC, 0xFE,
3536     // nonce proof
3537     0x89, 0x67, 0x45, 0x23,
3538     0x01, 0xEF, 0xCD, 0xAB,
3539     // rejected sequence number
3540     0xBC, 0x9A, 0x78, 0x56,
3541     0x34, 0x12,
3542   };
3543
3544   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3545   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3546   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
3547   ASSERT_TRUE(visitor_.public_reset_packet_.get());
3548   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
3549             visitor_.public_reset_packet_->public_header.connection_id);
3550   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
3551   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
3552   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
3553             visitor_.public_reset_packet_->nonce_proof);
3554   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
3555             visitor_.public_reset_packet_->rejected_sequence_number);
3556   EXPECT_TRUE(
3557       visitor_.public_reset_packet_->client_address.address().empty());
3558
3559   // Now test framing boundaries
3560   for (size_t i = 0; i < arraysize(packet); ++i) {
3561     string expected_error;
3562     DVLOG(1) << "iteration: " << i;
3563     if (i < kConnectionIdOffset) {
3564       expected_error = "Unable to read public flags.";
3565       CheckProcessingFails(packet, i, expected_error,
3566                            QUIC_INVALID_PACKET_HEADER);
3567     } else if (i < kPublicResetPacketNonceProofOffset) {
3568       expected_error = "Unable to read ConnectionId.";
3569       CheckProcessingFails(packet, i, expected_error,
3570                            QUIC_INVALID_PACKET_HEADER);
3571     } else if (i < kPublicResetPacketRejectedSequenceNumberOffset) {
3572       expected_error = "Unable to read nonce proof.";
3573       CheckProcessingFails(packet, i, expected_error,
3574                            QUIC_INVALID_PUBLIC_RST_PACKET);
3575     } else {
3576       expected_error = "Unable to read rejected sequence number.";
3577       CheckProcessingFails(packet, i, expected_error,
3578                            QUIC_INVALID_PUBLIC_RST_PACKET);
3579     }
3580   }
3581 }
3582
3583 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
3584   unsigned char packet[] = {
3585     // public flags (version, 8 byte connection_id)
3586     0x3D,
3587     // connection_id
3588     0x10, 0x32, 0x54, 0x76,
3589     0x98, 0xBA, 0xDC, 0xFE,
3590     // version tag
3591     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3592     'Q', '2', '.', '0',
3593   };
3594
3595   QuicFramerPeer::SetIsServer(&framer_, false);
3596
3597   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3598   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3599   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
3600   ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
3601   EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
3602   EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
3603
3604   for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID; ++i) {
3605     string expected_error;
3606     QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
3607     if (i < kConnectionIdOffset) {
3608       expected_error = "Unable to read public flags.";
3609     } else if (i < kVersionOffset) {
3610       expected_error = "Unable to read ConnectionId.";
3611     } else {
3612       expected_error = "Unable to read supported version in negotiation.";
3613       error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
3614     }
3615     CheckProcessingFails(packet, i, expected_error, error_code);
3616   }
3617 }
3618
3619 TEST_P(QuicFramerTest, FecPacket) {
3620   unsigned char packet[] = {
3621     // public flags (8 byte connection_id)
3622     0x3C,
3623     // connection_id
3624     0x10, 0x32, 0x54, 0x76,
3625     0x98, 0xBA, 0xDC, 0xFE,
3626     // packet sequence number
3627     0xBC, 0x9A, 0x78, 0x56,
3628     0x34, 0x12,
3629     // private flags (fec group & FEC)
3630     0x06,
3631     // first fec protected packet offset
3632     0x01,
3633
3634     // redundancy
3635     'a',  'b',  'c',  'd',
3636     'e',  'f',  'g',  'h',
3637     'i',  'j',  'k',  'l',
3638     'm',  'n',  'o',  'p',
3639   };
3640
3641   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3642   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3643
3644   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3645   ASSERT_TRUE(visitor_.header_.get());
3646   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
3647
3648   EXPECT_EQ(0u, visitor_.stream_frames_.size());
3649   EXPECT_EQ(0u, visitor_.ack_frames_.size());
3650   ASSERT_EQ(1, visitor_.fec_count_);
3651   const QuicFecData& fec_data = *visitor_.fec_data_[0];
3652   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
3653   EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
3654 }
3655
3656 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
3657   QuicPacketHeader header;
3658   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3659   header.public_header.reset_flag = false;
3660   header.public_header.version_flag = false;
3661   header.fec_flag = false;
3662   header.entropy_flag = false;
3663   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3664   header.fec_group = 0;
3665
3666   QuicPaddingFrame padding_frame;
3667
3668   QuicFrames frames;
3669   frames.push_back(QuicFrame(&padding_frame));
3670
3671   unsigned char packet[kMaxPacketSize] = {
3672     // public flags (8 byte connection_id)
3673     0x3C,
3674     // connection_id
3675     0x10, 0x32, 0x54, 0x76,
3676     0x98, 0xBA, 0xDC, 0xFE,
3677     // packet sequence number
3678     0xBC, 0x9A, 0x78, 0x56,
3679     0x34, 0x12,
3680     // private flags
3681     0x00,
3682
3683     // frame type (padding frame)
3684     0x00,
3685     0x00, 0x00, 0x00, 0x00
3686   };
3687
3688   uint64 header_size =
3689       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3690                           PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3691   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3692
3693   scoped_ptr<QuicPacket> data(
3694       framer_.BuildUnsizedDataPacket(header, frames).packet);
3695   ASSERT_TRUE(data != NULL);
3696
3697   test::CompareCharArraysWithHexError("constructed packet",
3698                                       data->data(), data->length(),
3699                                       AsChars(packet),
3700                                       arraysize(packet));
3701 }
3702
3703 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
3704   QuicPacketHeader header;
3705   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3706   header.public_header.reset_flag = false;
3707   header.public_header.version_flag = false;
3708   header.fec_flag = false;
3709   header.entropy_flag = false;
3710   header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
3711   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3712   header.fec_group = 0;
3713
3714   QuicPaddingFrame padding_frame;
3715
3716   QuicFrames frames;
3717   frames.push_back(QuicFrame(&padding_frame));
3718
3719   unsigned char packet[kMaxPacketSize] = {
3720     // public flags (8 byte connection_id and 4 byte sequence number)
3721     0x2C,
3722     // connection_id
3723     0x10, 0x32, 0x54, 0x76,
3724     0x98, 0xBA, 0xDC, 0xFE,
3725     // packet sequence number
3726     0xBC, 0x9A, 0x78, 0x56,
3727     // private flags
3728     0x00,
3729
3730     // frame type (padding frame)
3731     0x00,
3732     0x00, 0x00, 0x00, 0x00
3733   };
3734
3735   uint64 header_size =
3736       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3737                           PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3738   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3739
3740   scoped_ptr<QuicPacket> data(
3741       framer_.BuildUnsizedDataPacket(header, frames).packet);
3742   ASSERT_TRUE(data != NULL);
3743
3744   test::CompareCharArraysWithHexError("constructed packet",
3745                                       data->data(), data->length(),
3746                                       AsChars(packet),
3747                                       arraysize(packet));
3748 }
3749
3750 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
3751   QuicPacketHeader header;
3752   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3753   header.public_header.reset_flag = false;
3754   header.public_header.version_flag = false;
3755   header.fec_flag = false;
3756   header.entropy_flag = false;
3757   header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
3758   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3759   header.fec_group = 0;
3760
3761   QuicPaddingFrame padding_frame;
3762
3763   QuicFrames frames;
3764   frames.push_back(QuicFrame(&padding_frame));
3765
3766   unsigned char packet[kMaxPacketSize] = {
3767     // public flags (8 byte connection_id and 2 byte sequence number)
3768     0x1C,
3769     // connection_id
3770     0x10, 0x32, 0x54, 0x76,
3771     0x98, 0xBA, 0xDC, 0xFE,
3772     // packet sequence number
3773     0xBC, 0x9A,
3774     // private flags
3775     0x00,
3776
3777     // frame type (padding frame)
3778     0x00,
3779     0x00, 0x00, 0x00, 0x00
3780   };
3781
3782   uint64 header_size =
3783       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3784                           PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3785   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3786
3787   scoped_ptr<QuicPacket> data(
3788       framer_.BuildUnsizedDataPacket(header, frames).packet);
3789   ASSERT_TRUE(data != NULL);
3790
3791   test::CompareCharArraysWithHexError("constructed packet",
3792                                       data->data(), data->length(),
3793                                       AsChars(packet),
3794                                       arraysize(packet));
3795 }
3796
3797 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
3798   QuicPacketHeader header;
3799   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3800   header.public_header.reset_flag = false;
3801   header.public_header.version_flag = false;
3802   header.fec_flag = false;
3803   header.entropy_flag = false;
3804   header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
3805   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3806   header.fec_group = 0;
3807
3808   QuicPaddingFrame padding_frame;
3809
3810   QuicFrames frames;
3811   frames.push_back(QuicFrame(&padding_frame));
3812
3813   unsigned char packet[kMaxPacketSize] = {
3814     // public flags (8 byte connection_id and 1 byte sequence number)
3815     0x0C,
3816     // connection_id
3817     0x10, 0x32, 0x54, 0x76,
3818     0x98, 0xBA, 0xDC, 0xFE,
3819     // packet sequence number
3820     0xBC,
3821     // private flags
3822     0x00,
3823
3824     // frame type (padding frame)
3825     0x00,
3826     0x00, 0x00, 0x00, 0x00
3827   };
3828
3829   uint64 header_size =
3830       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3831                           PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
3832   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
3833
3834   scoped_ptr<QuicPacket> data(
3835       framer_.BuildUnsizedDataPacket(header, frames).packet);
3836   ASSERT_TRUE(data != NULL);
3837
3838   test::CompareCharArraysWithHexError("constructed packet",
3839                                       data->data(), data->length(),
3840                                       AsChars(packet),
3841                                       arraysize(packet));
3842 }
3843
3844 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
3845   QuicPacketHeader header;
3846   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3847   header.public_header.reset_flag = false;
3848   header.public_header.version_flag = false;
3849   header.fec_flag = false;
3850   header.entropy_flag = true;
3851   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
3852   header.fec_group = 0;
3853
3854   QuicStreamFrame stream_frame;
3855   stream_frame.stream_id = 0x01020304;
3856   stream_frame.fin = true;
3857   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
3858   stream_frame.data = MakeIOVector("hello world!");
3859
3860   QuicFrames frames;
3861   frames.push_back(QuicFrame(&stream_frame));
3862
3863   unsigned char packet[] = {
3864     // public flags (8 byte connection_id)
3865     0x3C,
3866     // connection_id
3867     0x10, 0x32, 0x54, 0x76,
3868     0x98, 0xBA, 0xDC, 0xFE,
3869     // packet sequence number
3870     0xBC, 0x9A, 0x78, 0x56,
3871     0x34, 0x12,
3872     // private flags (entropy)
3873     0x01,
3874
3875     // frame type (stream frame with fin and no length)
3876     0xDF,
3877     // stream id
3878     0x04, 0x03, 0x02, 0x01,
3879     // offset
3880     0x54, 0x76, 0x10, 0x32,
3881     0xDC, 0xFE, 0x98, 0xBA,
3882     // data
3883     'h',  'e',  'l',  'l',
3884     'o',  ' ',  'w',  'o',
3885     'r',  'l',  'd',  '!',
3886   };
3887
3888   scoped_ptr<QuicPacket> data(
3889       framer_.BuildUnsizedDataPacket(header, frames).packet);
3890   ASSERT_TRUE(data != NULL);
3891
3892   test::CompareCharArraysWithHexError("constructed packet",
3893                                       data->data(), data->length(),
3894                                       AsChars(packet), arraysize(packet));
3895 }
3896
3897 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
3898   QuicPacketHeader header;
3899   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3900   header.public_header.reset_flag = false;
3901   header.public_header.version_flag = true;
3902   header.fec_flag = false;
3903   header.entropy_flag = true;
3904   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
3905   header.fec_group = 0;
3906
3907   QuicStreamFrame stream_frame;
3908   stream_frame.stream_id = 0x01020304;
3909   stream_frame.fin = true;
3910   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
3911   stream_frame.data = MakeIOVector("hello world!");
3912
3913   QuicFrames frames;
3914   frames.push_back(QuicFrame(&stream_frame));
3915
3916   unsigned char packet[] = {
3917     // public flags (version, 8 byte connection_id)
3918     0x3D,
3919     // connection_id
3920     0x10, 0x32, 0x54, 0x76,
3921     0x98, 0xBA, 0xDC, 0xFE,
3922     // version tag
3923     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3924     // packet sequence number
3925     0xBC, 0x9A, 0x78, 0x56,
3926     0x34, 0x12,
3927     // private flags (entropy)
3928     0x01,
3929
3930     // frame type (stream frame with fin and no length)
3931     0xDF,
3932     // stream id
3933     0x04, 0x03, 0x02, 0x01,
3934     // offset
3935     0x54, 0x76, 0x10, 0x32,
3936     0xDC, 0xFE, 0x98, 0xBA,
3937     // data
3938     'h',  'e',  'l',  'l',
3939     'o',  ' ',  'w',  'o',
3940     'r',  'l',  'd',  '!',
3941   };
3942
3943   QuicFramerPeer::SetIsServer(&framer_, false);
3944   scoped_ptr<QuicPacket> data(
3945       framer_.BuildUnsizedDataPacket(header, frames).packet);
3946   ASSERT_TRUE(data != NULL);
3947
3948   test::CompareCharArraysWithHexError("constructed packet",
3949                                       data->data(), data->length(),
3950                                       AsChars(packet), arraysize(packet));
3951 }
3952
3953 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
3954   QuicPacketPublicHeader header;
3955   header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3956   header.reset_flag = false;
3957   header.version_flag = true;
3958
3959   unsigned char packet[] = {
3960     // public flags (version, 8 byte connection_id)
3961     0x0D,
3962     // connection_id
3963     0x10, 0x32, 0x54, 0x76,
3964     0x98, 0xBA, 0xDC, 0xFE,
3965     // version tag
3966     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3967   };
3968
3969   QuicVersionVector versions;
3970   versions.push_back(GetParam());
3971   scoped_ptr<QuicEncryptedPacket> data(
3972       framer_.BuildVersionNegotiationPacket(header, versions));
3973
3974   test::CompareCharArraysWithHexError("constructed packet",
3975                                       data->data(), data->length(),
3976                                       AsChars(packet), arraysize(packet));
3977 }
3978
3979 TEST_P(QuicFramerTest, BuildAckFramePacket) {
3980   if (version_ <= QUIC_VERSION_15) {
3981     return;
3982   }
3983   QuicPacketHeader header;
3984   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3985   header.public_header.reset_flag = false;
3986   header.public_header.version_flag = false;
3987   header.fec_flag = false;
3988   header.entropy_flag = true;
3989   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3990   header.fec_group = 0;
3991
3992   QuicAckFrame ack_frame;
3993   ack_frame.received_info.entropy_hash = 0x43;
3994   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
3995   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
3996   ack_frame.received_info.missing_packets.insert(
3997       GG_UINT64_C(0x770123456789ABE));
3998
3999   QuicFrames frames;
4000   frames.push_back(QuicFrame(&ack_frame));
4001
4002   unsigned char packet[] = {
4003     // public flags (8 byte connection_id)
4004     0x3C,
4005     // connection_id
4006     0x10, 0x32, 0x54, 0x76,
4007     0x98, 0xBA, 0xDC, 0xFE,
4008     // packet sequence number
4009     0xA8, 0x9A, 0x78, 0x56,
4010     0x34, 0x12,
4011     // private flags (entropy)
4012     0x01,
4013
4014     // frame type (ack frame)
4015     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
4016     0x6C,
4017     // entropy hash of all received packets.
4018     0x43,
4019     // largest observed packet sequence number
4020     0xBF, 0x9A, 0x78, 0x56,
4021     0x34, 0x12,
4022     // Zero delta time.
4023     0x0, 0x0,
4024     // num missing packet ranges
4025     0x01,
4026     // missing packet delta
4027     0x01,
4028     // 0 more missing packets in range.
4029     0x00,
4030     // 0 revived packets.
4031     0x00,
4032   };
4033
4034   scoped_ptr<QuicPacket> data(
4035       framer_.BuildUnsizedDataPacket(header, frames).packet);
4036   ASSERT_TRUE(data != NULL);
4037
4038   test::CompareCharArraysWithHexError("constructed packet",
4039                                       data->data(), data->length(),
4040                                       AsChars(packet), arraysize(packet));
4041 }
4042
4043 TEST_P(QuicFramerTest, BuildAckFramePacket15) {
4044   if (version_ != QUIC_VERSION_15) {
4045     return;
4046   }
4047   QuicPacketHeader header;
4048   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4049   header.public_header.reset_flag = false;
4050   header.public_header.version_flag = false;
4051   header.fec_flag = false;
4052   header.entropy_flag = true;
4053   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
4054   header.fec_group = 0;
4055
4056   QuicAckFrame ack_frame;
4057   ack_frame.received_info.entropy_hash = 0x43;
4058   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
4059   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
4060   ack_frame.received_info.missing_packets.insert(
4061       GG_UINT64_C(0x770123456789ABE));
4062   ack_frame.sent_info.entropy_hash = 0x14;
4063   ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
4064
4065   QuicFrames frames;
4066   frames.push_back(QuicFrame(&ack_frame));
4067
4068   unsigned char packet[] = {
4069     // public flags (8 byte connection_id)
4070     0x3C,
4071     // connection_id
4072     0x10, 0x32, 0x54, 0x76,
4073     0x98, 0xBA, 0xDC, 0xFE,
4074     // packet sequence number
4075     0xA8, 0x9A, 0x78, 0x56,
4076     0x34, 0x12,
4077     // private flags (entropy)
4078     0x01,
4079
4080     // frame type (ack frame)
4081     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
4082     0x6C,
4083     // entropy hash of sent packets till least awaiting - 1.
4084     0x14,
4085     // least packet sequence number awaiting an ack, delta from sequence number.
4086     0x08, 0x00, 0x00, 0x00,
4087     0x00, 0x00,
4088     // entropy hash of all received packets.
4089     0x43,
4090     // largest observed packet sequence number
4091     0xBF, 0x9A, 0x78, 0x56,
4092     0x34, 0x12,
4093     // Zero delta time.
4094     0x0, 0x0,
4095     // num missing packet ranges
4096     0x01,
4097     // missing packet delta
4098     0x01,
4099     // 0 more missing packets in range.
4100     0x00,
4101     // 0 revived packets.
4102     0x00,
4103   };
4104
4105   scoped_ptr<QuicPacket> data(
4106       framer_.BuildUnsizedDataPacket(header, frames).packet);
4107   ASSERT_TRUE(data != NULL);
4108
4109   test::CompareCharArraysWithHexError("constructed packet",
4110                                       data->data(), data->length(),
4111                                       AsChars(packet), arraysize(packet));
4112 }
4113
4114 TEST_P(QuicFramerTest, BuildAckFramePacketV13) {
4115   if (version_ != QUIC_VERSION_13) {
4116     return;
4117   }
4118   QuicPacketHeader header;
4119   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4120   header.public_header.reset_flag = false;
4121   header.public_header.version_flag = false;
4122   header.fec_flag = false;
4123   header.entropy_flag = true;
4124   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
4125   header.fec_group = 0;
4126
4127   QuicAckFrame ack_frame;
4128   ack_frame.received_info.entropy_hash = 0x43;
4129   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
4130   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
4131   ack_frame.received_info.missing_packets.insert(
4132       GG_UINT64_C(0x770123456789ABE));
4133   ack_frame.sent_info.entropy_hash = 0x14;
4134   ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
4135
4136   QuicFrames frames;
4137   frames.push_back(QuicFrame(&ack_frame));
4138
4139   unsigned char packet[] = {
4140     // public flags (8 byte connection_id)
4141     0x3C,
4142     // connection_id
4143     0x10, 0x32, 0x54, 0x76,
4144     0x98, 0xBA, 0xDC, 0xFE,
4145     // packet sequence number
4146     0xA8, 0x9A, 0x78, 0x56,
4147     0x34, 0x12,
4148     // private flags (entropy)
4149     0x01,
4150
4151     // frame type (ack frame)
4152     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
4153     0x6C,
4154     // entropy hash of sent packets till least awaiting - 1.
4155     0x14,
4156     // least packet sequence number awaiting an ack, delta from sequence number.
4157     0x08, 0x00, 0x00, 0x00,
4158     0x00, 0x00,
4159     // entropy hash of all received packets.
4160     0x43,
4161     // largest observed packet sequence number
4162     0xBF, 0x9A, 0x78, 0x56,
4163     0x34, 0x12,
4164     // Zero delta time.
4165     0x0, 0x0,
4166     // num missing packet ranges
4167     0x01,
4168     // missing packet delta
4169     0x01,
4170     // 0 more missing packets in range.
4171     0x00,
4172   };
4173
4174   scoped_ptr<QuicPacket> data(
4175       framer_.BuildUnsizedDataPacket(header, frames).packet);
4176   ASSERT_TRUE(data != NULL);
4177
4178   test::CompareCharArraysWithHexError("constructed packet",
4179                                       data->data(), data->length(),
4180                                       AsChars(packet), arraysize(packet));
4181 }
4182
4183 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCP) {
4184   if (version_ == QUIC_VERSION_13) {
4185     return;
4186   }
4187   QuicPacketHeader header;
4188   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4189   header.public_header.reset_flag = false;
4190   header.public_header.version_flag = false;
4191   header.fec_flag = false;
4192   header.entropy_flag = false;
4193   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4194   header.fec_group = 0;
4195
4196   QuicCongestionFeedbackFrame congestion_feedback_frame;
4197   congestion_feedback_frame.type = kTCP;
4198   congestion_feedback_frame.tcp.receive_window = 0x4030;
4199
4200   QuicFrames frames;
4201   frames.push_back(QuicFrame(&congestion_feedback_frame));
4202
4203   unsigned char packet[] = {
4204     // public flags (8 byte connection_id)
4205     0x3C,
4206     // connection_id
4207     0x10, 0x32, 0x54, 0x76,
4208     0x98, 0xBA, 0xDC, 0xFE,
4209     // packet sequence number
4210     0xBC, 0x9A, 0x78, 0x56,
4211     0x34, 0x12,
4212     // private flags
4213     0x00,
4214
4215     // frame type (congestion feedback frame)
4216     0x20,
4217     // congestion feedback type (TCP)
4218     0x00,
4219     // TCP receive window
4220     0x03, 0x04,
4221   };
4222
4223   scoped_ptr<QuicPacket> data(
4224       framer_.BuildUnsizedDataPacket(header, frames).packet);
4225   ASSERT_TRUE(data != NULL);
4226
4227   test::CompareCharArraysWithHexError("constructed packet",
4228                                       data->data(), data->length(),
4229                                       AsChars(packet), arraysize(packet));
4230 }
4231
4232 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCPV13) {
4233   if (version_ != QUIC_VERSION_13) {
4234     return;
4235   }
4236   QuicPacketHeader header;
4237   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4238   header.public_header.reset_flag = false;
4239   header.public_header.version_flag = false;
4240   header.fec_flag = false;
4241   header.entropy_flag = false;
4242   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4243   header.fec_group = 0;
4244
4245   QuicCongestionFeedbackFrame congestion_feedback_frame;
4246   congestion_feedback_frame.type = kTCP;
4247   congestion_feedback_frame.tcp.receive_window = 0x4030;
4248
4249   QuicFrames frames;
4250   frames.push_back(QuicFrame(&congestion_feedback_frame));
4251
4252   unsigned char packet[] = {
4253     // public flags (8 byte connection_id)
4254     0x3C,
4255     // connection_id
4256     0x10, 0x32, 0x54, 0x76,
4257     0x98, 0xBA, 0xDC, 0xFE,
4258     // packet sequence number
4259     0xBC, 0x9A, 0x78, 0x56,
4260     0x34, 0x12,
4261     // private flags
4262     0x00,
4263
4264     // frame type (congestion feedback frame)
4265     0x20,
4266     // congestion feedback type (TCP)
4267     0x00,
4268     // accumulated number of lost packets
4269     0x00, 0x00,
4270     // TCP receive window
4271     0x03, 0x04,
4272   };
4273
4274   scoped_ptr<QuicPacket> data(
4275       framer_.BuildUnsizedDataPacket(header, frames).packet);
4276   ASSERT_TRUE(data != NULL);
4277
4278   test::CompareCharArraysWithHexError("constructed packet",
4279                                       data->data(), data->length(),
4280                                       AsChars(packet), arraysize(packet));
4281 }
4282
4283 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInterArrival) {
4284   if (version_ == QUIC_VERSION_13) {
4285     return;
4286   }
4287   QuicPacketHeader header;
4288   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4289   header.public_header.reset_flag = false;
4290   header.public_header.version_flag = false;
4291   header.fec_flag = false;
4292   header.entropy_flag = false;
4293   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4294   header.fec_group = 0;
4295
4296   QuicCongestionFeedbackFrame frame;
4297   frame.type = kInterArrival;
4298   frame.inter_arrival.received_packet_times.insert(
4299       make_pair(GG_UINT64_C(0x0123456789ABA),
4300                 start_.Add(QuicTime::Delta::FromMicroseconds(
4301                     GG_UINT64_C(0x07E1D2C3B4A59687)))));
4302   frame.inter_arrival.received_packet_times.insert(
4303       make_pair(GG_UINT64_C(0x0123456789ABB),
4304                 start_.Add(QuicTime::Delta::FromMicroseconds(
4305                     GG_UINT64_C(0x07E1D2C3B4A59688)))));
4306   frame.inter_arrival.received_packet_times.insert(
4307       make_pair(GG_UINT64_C(0x0123456789ABD),
4308                 start_.Add(QuicTime::Delta::FromMicroseconds(
4309                     GG_UINT64_C(0x07E1D2C3B4A59689)))));
4310   QuicFrames frames;
4311   frames.push_back(QuicFrame(&frame));
4312
4313   unsigned char packet[] = {
4314     // public flags (8 byte connection_id)
4315     0x3C,
4316     // connection_id
4317     0x10, 0x32, 0x54, 0x76,
4318     0x98, 0xBA, 0xDC, 0xFE,
4319     // packet sequence number
4320     0xBC, 0x9A, 0x78, 0x56,
4321     0x34, 0x12,
4322     // private flags
4323     0x00,
4324
4325     // frame type (congestion feedback frame)
4326     0x20,
4327     // congestion feedback type (inter arrival)
4328     0x01,
4329     // num received packets
4330     0x03,
4331     // lowest sequence number
4332     0xBA, 0x9A, 0x78, 0x56,
4333     0x34, 0x12,
4334     // receive time
4335     0x87, 0x96, 0xA5, 0xB4,
4336     0xC3, 0xD2, 0xE1, 0x07,
4337     // sequence delta
4338     0x01, 0x00,
4339     // time delta
4340     0x01, 0x00, 0x00, 0x00,
4341     // sequence delta (skip one packet)
4342     0x03, 0x00,
4343     // time delta
4344     0x02, 0x00, 0x00, 0x00,
4345   };
4346
4347   scoped_ptr<QuicPacket> data(
4348       framer_.BuildUnsizedDataPacket(header, frames).packet);
4349   ASSERT_TRUE(data != NULL);
4350
4351   test::CompareCharArraysWithHexError("constructed packet",
4352                                       data->data(), data->length(),
4353                                       AsChars(packet), arraysize(packet));
4354 }
4355
4356 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInterArrivalV13) {
4357   if (version_ != QUIC_VERSION_13) {
4358     return;
4359   }
4360   QuicPacketHeader header;
4361   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4362   header.public_header.reset_flag = false;
4363   header.public_header.version_flag = false;
4364   header.fec_flag = false;
4365   header.entropy_flag = false;
4366   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4367   header.fec_group = 0;
4368
4369   QuicCongestionFeedbackFrame frame;
4370   frame.type = kInterArrival;
4371   frame.inter_arrival.received_packet_times.insert(
4372       make_pair(GG_UINT64_C(0x0123456789ABA),
4373                 start_.Add(QuicTime::Delta::FromMicroseconds(
4374                     GG_UINT64_C(0x07E1D2C3B4A59687)))));
4375   frame.inter_arrival.received_packet_times.insert(
4376       make_pair(GG_UINT64_C(0x0123456789ABB),
4377                 start_.Add(QuicTime::Delta::FromMicroseconds(
4378                     GG_UINT64_C(0x07E1D2C3B4A59688)))));
4379   frame.inter_arrival.received_packet_times.insert(
4380       make_pair(GG_UINT64_C(0x0123456789ABD),
4381                 start_.Add(QuicTime::Delta::FromMicroseconds(
4382                     GG_UINT64_C(0x07E1D2C3B4A59689)))));
4383   QuicFrames frames;
4384   frames.push_back(QuicFrame(&frame));
4385
4386   unsigned char packet[] = {
4387     // public flags (8 byte connection_id)
4388     0x3C,
4389     // connection_id
4390     0x10, 0x32, 0x54, 0x76,
4391     0x98, 0xBA, 0xDC, 0xFE,
4392     // packet sequence number
4393     0xBC, 0x9A, 0x78, 0x56,
4394     0x34, 0x12,
4395     // private flags
4396     0x00,
4397
4398     // frame type (congestion feedback frame)
4399     0x20,
4400     // congestion feedback type (inter arrival)
4401     0x01,
4402     // accumulated_number_of_lost_packets
4403     0x00, 0x00,
4404     // num received packets
4405     0x03,
4406     // lowest sequence number
4407     0xBA, 0x9A, 0x78, 0x56,
4408     0x34, 0x12,
4409     // receive time
4410     0x87, 0x96, 0xA5, 0xB4,
4411     0xC3, 0xD2, 0xE1, 0x07,
4412     // sequence delta
4413     0x01, 0x00,
4414     // time delta
4415     0x01, 0x00, 0x00, 0x00,
4416     // sequence delta (skip one packet)
4417     0x03, 0x00,
4418     // time delta
4419     0x02, 0x00, 0x00, 0x00,
4420   };
4421
4422   scoped_ptr<QuicPacket> data(
4423       framer_.BuildUnsizedDataPacket(header, frames).packet);
4424   ASSERT_TRUE(data != NULL);
4425
4426   test::CompareCharArraysWithHexError("constructed packet",
4427                                       data->data(), data->length(),
4428                                       AsChars(packet), arraysize(packet));
4429 }
4430
4431 TEST_P(QuicFramerTest, BuildStopWaitingPacket) {
4432   if (version_ <= QUIC_VERSION_15) {
4433     return;
4434   }
4435   QuicPacketHeader header;
4436   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4437   header.public_header.reset_flag = false;
4438   header.public_header.version_flag = false;
4439   header.fec_flag = false;
4440   header.entropy_flag = true;
4441   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
4442   header.fec_group = 0;
4443
4444   QuicStopWaitingFrame stop_waiting_frame;
4445   stop_waiting_frame.entropy_hash = 0x14;
4446   stop_waiting_frame.least_unacked = GG_UINT64_C(0x770123456789AA0);
4447
4448   QuicFrames frames;
4449   frames.push_back(QuicFrame(&stop_waiting_frame));
4450
4451   unsigned char packet[] = {
4452     // public flags (8 byte connection_id)
4453     0x3C,
4454     // connection_id
4455     0x10, 0x32, 0x54, 0x76,
4456     0x98, 0xBA, 0xDC, 0xFE,
4457     // packet sequence number
4458     0xA8, 0x9A, 0x78, 0x56,
4459     0x34, 0x12,
4460     // private flags (entropy)
4461     0x01,
4462
4463     // frame type (stop waiting frame)
4464     0x06,
4465     // entropy hash of sent packets till least awaiting - 1.
4466     0x14,
4467     // least packet sequence number awaiting an ack, delta from sequence number.
4468     0x08, 0x00, 0x00, 0x00,
4469     0x00, 0x00,
4470   };
4471
4472   scoped_ptr<QuicPacket> data(
4473       framer_.BuildUnsizedDataPacket(header, frames).packet);
4474   ASSERT_TRUE(data != NULL);
4475
4476   test::CompareCharArraysWithHexError("constructed packet",
4477                                       data->data(), data->length(),
4478                                       AsChars(packet), arraysize(packet));
4479 }
4480
4481 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketFixRate) {
4482   QuicPacketHeader header;
4483   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4484   header.public_header.reset_flag = false;
4485   header.public_header.version_flag = false;
4486   header.fec_flag = false;
4487   header.entropy_flag = false;
4488   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4489   header.fec_group = 0;
4490
4491   QuicCongestionFeedbackFrame congestion_feedback_frame;
4492   congestion_feedback_frame.type = kFixRate;
4493   congestion_feedback_frame.fix_rate.bitrate
4494       = QuicBandwidth::FromBytesPerSecond(0x04030201);
4495
4496   QuicFrames frames;
4497   frames.push_back(QuicFrame(&congestion_feedback_frame));
4498
4499   unsigned char packet[] = {
4500     // public flags (8 byte connection_id)
4501     0x3C,
4502     // connection_id
4503     0x10, 0x32, 0x54, 0x76,
4504     0x98, 0xBA, 0xDC, 0xFE,
4505     // packet sequence number
4506     0xBC, 0x9A, 0x78, 0x56,
4507     0x34, 0x12,
4508     // private flags
4509     0x00,
4510
4511     // frame type (congestion feedback frame)
4512     0x20,
4513     // congestion feedback type (fix rate)
4514     0x02,
4515     // bitrate_in_bytes_per_second;
4516     0x01, 0x02, 0x03, 0x04,
4517   };
4518
4519   scoped_ptr<QuicPacket> data(
4520       framer_.BuildUnsizedDataPacket(header, frames).packet);
4521   ASSERT_TRUE(data != NULL);
4522
4523   test::CompareCharArraysWithHexError("constructed packet",
4524                                       data->data(), data->length(),
4525                                       AsChars(packet), arraysize(packet));
4526 }
4527
4528 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInvalidFeedback) {
4529   QuicPacketHeader header;
4530   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4531   header.public_header.reset_flag = false;
4532   header.public_header.version_flag = false;
4533   header.fec_flag = false;
4534   header.entropy_flag = false;
4535   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4536   header.fec_group = 0;
4537
4538   QuicCongestionFeedbackFrame congestion_feedback_frame;
4539   congestion_feedback_frame.type =
4540       static_cast<CongestionFeedbackType>(kFixRate + 1);
4541
4542   QuicFrames frames;
4543   frames.push_back(QuicFrame(&congestion_feedback_frame));
4544
4545   scoped_ptr<QuicPacket> data;
4546   EXPECT_DFATAL(
4547       data.reset(framer_.BuildUnsizedDataPacket(header, frames).packet),
4548       "AppendCongestionFeedbackFrame failed");
4549   ASSERT_TRUE(data == NULL);
4550 }
4551
4552 TEST_P(QuicFramerTest, BuildRstFramePacketVersion13) {
4553   if (version_ > QUIC_VERSION_13) {
4554     return;
4555   }
4556
4557   QuicPacketHeader header;
4558   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4559   header.public_header.reset_flag = false;
4560   header.public_header.version_flag = false;
4561   header.fec_flag = false;
4562   header.entropy_flag = false;
4563   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4564   header.fec_group = 0;
4565
4566   QuicRstStreamFrame rst_frame;
4567   rst_frame.stream_id = 0x01020304;
4568   rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
4569   rst_frame.error_details = "because I can";
4570
4571   unsigned char packet[] = {
4572     // public flags (8 byte connection_id)
4573     0x3C,
4574     // connection_id
4575     0x10, 0x32, 0x54, 0x76,
4576     0x98, 0xBA, 0xDC, 0xFE,
4577     // packet sequence number
4578     0xBC, 0x9A, 0x78, 0x56,
4579     0x34, 0x12,
4580     // private flags
4581     0x00,
4582
4583     // frame type (rst stream frame)
4584     0x01,
4585     // stream id
4586     0x04, 0x03, 0x02, 0x01,
4587     // error code
4588     0x08, 0x07, 0x06, 0x05,
4589     // error details length
4590     0x0d, 0x00,
4591     // error details
4592     'b',  'e',  'c',  'a',
4593     'u',  's',  'e',  ' ',
4594     'I',  ' ',  'c',  'a',
4595     'n',
4596   };
4597
4598   QuicFrames frames;
4599   frames.push_back(QuicFrame(&rst_frame));
4600
4601   scoped_ptr<QuicPacket> data(
4602       framer_.BuildUnsizedDataPacket(header, frames).packet);
4603   ASSERT_TRUE(data != NULL);
4604
4605   test::CompareCharArraysWithHexError("constructed packet",
4606                                       data->data(), data->length(),
4607                                       AsChars(packet), arraysize(packet));
4608 }
4609
4610 TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
4611   if (version_ <= QUIC_VERSION_13) {
4612     return;
4613   }
4614
4615   QuicPacketHeader header;
4616   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4617   header.public_header.reset_flag = false;
4618   header.public_header.version_flag = false;
4619   header.fec_flag = false;
4620   header.entropy_flag = false;
4621   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4622   header.fec_group = 0;
4623
4624   QuicRstStreamFrame rst_frame;
4625   rst_frame.stream_id = 0x01020304;
4626   rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
4627   rst_frame.error_details = "because I can";
4628   rst_frame.byte_offset = 0x0807060504030201;
4629
4630   unsigned char packet[] = {
4631     // public flags (8 byte connection_id)
4632     0x3C,
4633     // connection_id
4634     0x10, 0x32, 0x54, 0x76,
4635     0x98, 0xBA, 0xDC, 0xFE,
4636     // packet sequence number
4637     0xBC, 0x9A, 0x78, 0x56,
4638     0x34, 0x12,
4639     // private flags
4640     0x00,
4641
4642     // frame type (rst stream frame)
4643     0x01,
4644     // stream id
4645     0x04, 0x03, 0x02, 0x01,
4646     // sent byte offset
4647     0x01, 0x02, 0x03, 0x04,
4648     0x05, 0x06, 0x07, 0x08,
4649     // error code
4650     0x08, 0x07, 0x06, 0x05,
4651     // error details length
4652     0x0d, 0x00,
4653     // error details
4654     'b',  'e',  'c',  'a',
4655     'u',  's',  'e',  ' ',
4656     'I',  ' ',  'c',  'a',
4657     'n',
4658   };
4659
4660   QuicFrames frames;
4661   frames.push_back(QuicFrame(&rst_frame));
4662
4663   scoped_ptr<QuicPacket> data(
4664       framer_.BuildUnsizedDataPacket(header, frames).packet);
4665   ASSERT_TRUE(data != NULL);
4666
4667   test::CompareCharArraysWithHexError("constructed packet",
4668                                       data->data(), data->length(),
4669                                       AsChars(packet), arraysize(packet));
4670 }
4671
4672 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
4673   QuicPacketHeader header;
4674   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4675   header.public_header.reset_flag = false;
4676   header.public_header.version_flag = false;
4677   header.fec_flag = false;
4678   header.entropy_flag = true;
4679   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4680   header.fec_group = 0;
4681
4682   QuicConnectionCloseFrame close_frame;
4683   close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
4684   close_frame.error_details = "because I can";
4685
4686   QuicFrames frames;
4687   frames.push_back(QuicFrame(&close_frame));
4688
4689   unsigned char packet[] = {
4690     // public flags (8 byte connection_id)
4691     0x3C,
4692     // connection_id
4693     0x10, 0x32, 0x54, 0x76,
4694     0x98, 0xBA, 0xDC, 0xFE,
4695     // packet sequence number
4696     0xBC, 0x9A, 0x78, 0x56,
4697     0x34, 0x12,
4698     // private flags (entropy)
4699     0x01,
4700
4701     // frame type (connection close frame)
4702     0x02,
4703     // error code
4704     0x08, 0x07, 0x06, 0x05,
4705     // error details length
4706     0x0d, 0x00,
4707     // error details
4708     'b',  'e',  'c',  'a',
4709     'u',  's',  'e',  ' ',
4710     'I',  ' ',  'c',  'a',
4711     'n',
4712   };
4713
4714   scoped_ptr<QuicPacket> data(
4715       framer_.BuildUnsizedDataPacket(header, frames).packet);
4716   ASSERT_TRUE(data != NULL);
4717
4718   test::CompareCharArraysWithHexError("constructed packet",
4719                                       data->data(), data->length(),
4720                                       AsChars(packet), arraysize(packet));
4721 }
4722
4723 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
4724   QuicPacketHeader header;
4725   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4726   header.public_header.reset_flag = false;
4727   header.public_header.version_flag = false;
4728   header.fec_flag = false;
4729   header.entropy_flag = true;
4730   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4731   header.fec_group = 0;
4732
4733   QuicGoAwayFrame goaway_frame;
4734   goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
4735   goaway_frame.last_good_stream_id = 0x01020304;
4736   goaway_frame.reason_phrase = "because I can";
4737
4738   QuicFrames frames;
4739   frames.push_back(QuicFrame(&goaway_frame));
4740
4741   unsigned char packet[] = {
4742     // public flags (8 byte connection_id)
4743     0x3C,
4744     // connection_id
4745     0x10, 0x32, 0x54, 0x76,
4746     0x98, 0xBA, 0xDC, 0xFE,
4747     // packet sequence number
4748     0xBC, 0x9A, 0x78, 0x56,
4749     0x34, 0x12,
4750     // private flags(entropy)
4751     0x01,
4752
4753     // frame type (go away frame)
4754     0x03,
4755     // error code
4756     0x08, 0x07, 0x06, 0x05,
4757     // stream id
4758     0x04, 0x03, 0x02, 0x01,
4759     // error details length
4760     0x0d, 0x00,
4761     // error details
4762     'b',  'e',  'c',  'a',
4763     'u',  's',  'e',  ' ',
4764     'I',  ' ',  'c',  'a',
4765     'n',
4766   };
4767
4768   scoped_ptr<QuicPacket> data(
4769       framer_.BuildUnsizedDataPacket(header, frames).packet);
4770   ASSERT_TRUE(data != NULL);
4771
4772   test::CompareCharArraysWithHexError("constructed packet",
4773                                       data->data(), data->length(),
4774                                       AsChars(packet), arraysize(packet));
4775 }
4776
4777 TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
4778   QuicPacketHeader header;
4779   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4780   header.public_header.reset_flag = false;
4781   header.public_header.version_flag = false;
4782   header.fec_flag = false;
4783   header.entropy_flag = true;
4784   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4785   header.fec_group = 0;
4786
4787   QuicWindowUpdateFrame window_update_frame;
4788   window_update_frame.stream_id = 0x01020304;
4789   window_update_frame.byte_offset = 0x1122334455667788;
4790
4791   QuicFrames frames;
4792   frames.push_back(QuicFrame(&window_update_frame));
4793
4794   unsigned char packet[] = {
4795     // public flags (8 byte connection_id)
4796     0x3C,
4797     // connection_id
4798     0x10, 0x32, 0x54, 0x76,
4799     0x98, 0xBA, 0xDC, 0xFE,
4800     // packet sequence number
4801     0xBC, 0x9A, 0x78, 0x56,
4802     0x34, 0x12,
4803     // private flags(entropy)
4804     0x01,
4805
4806     // frame type (window update frame)
4807     0x04,
4808     // stream id
4809     0x04, 0x03, 0x02, 0x01,
4810     // byte offset
4811     0x88, 0x77, 0x66, 0x55,
4812     0x44, 0x33, 0x22, 0x11,
4813   };
4814
4815   if (version_ > QUIC_VERSION_13) {
4816     scoped_ptr<QuicPacket> data(
4817         framer_.BuildUnsizedDataPacket(header, frames).packet);
4818     ASSERT_TRUE(data != NULL);
4819
4820     test::CompareCharArraysWithHexError("constructed packet", data->data(),
4821                                         data->length(), AsChars(packet),
4822                                         arraysize(packet));
4823   } else {
4824     string expected_error = "Attempt to add a WindowUpdateFrame in " +
4825                             QuicVersionToString(version_);
4826     EXPECT_DFATAL(framer_.BuildUnsizedDataPacket(header, frames),
4827                   expected_error);
4828     return;
4829   }
4830 }
4831
4832 TEST_P(QuicFramerTest, BuildBlockedPacket) {
4833   QuicPacketHeader header;
4834   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4835   header.public_header.reset_flag = false;
4836   header.public_header.version_flag = false;
4837   header.fec_flag = false;
4838   header.entropy_flag = true;
4839   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
4840   header.fec_group = 0;
4841
4842   QuicBlockedFrame blocked_frame;
4843   blocked_frame.stream_id = 0x01020304;
4844
4845   QuicFrames frames;
4846   frames.push_back(QuicFrame(&blocked_frame));
4847
4848   unsigned char packet[] = {
4849     // public flags (8 byte connection_id)
4850     0x3C,
4851     // connection_id
4852     0x10, 0x32, 0x54, 0x76,
4853     0x98, 0xBA, 0xDC, 0xFE,
4854     // packet sequence number
4855     0xBC, 0x9A, 0x78, 0x56,
4856     0x34, 0x12,
4857     // private flags(entropy)
4858     0x01,
4859
4860     // frame type (blocked frame)
4861     0x05,
4862     // stream id
4863     0x04, 0x03, 0x02, 0x01,
4864   };
4865
4866   if (version_ > QUIC_VERSION_13) {
4867     scoped_ptr<QuicPacket> data(
4868         framer_.BuildUnsizedDataPacket(header, frames).packet);
4869     ASSERT_TRUE(data != NULL);
4870
4871     test::CompareCharArraysWithHexError("constructed packet", data->data(),
4872                                         data->length(), AsChars(packet),
4873                                         arraysize(packet));
4874   } else {
4875     string expected_error =
4876         "Attempt to add a BlockedFrame in " + QuicVersionToString(version_);
4877     EXPECT_DFATAL(framer_.BuildUnsizedDataPacket(header, frames),
4878                   expected_error);
4879     return;
4880   }
4881 }
4882
4883 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
4884   QuicPublicResetPacket reset_packet;
4885   reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4886   reset_packet.public_header.reset_flag = true;
4887   reset_packet.public_header.version_flag = false;
4888   reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
4889   reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
4890
4891   unsigned char packet[] = {
4892     // public flags (public reset, 8 byte ConnectionId)
4893     0x0E,
4894     // connection_id
4895     0x10, 0x32, 0x54, 0x76,
4896     0x98, 0xBA, 0xDC, 0xFE,
4897     // message tag (kPRST)
4898     'P', 'R', 'S', 'T',
4899     // num_entries (2) + padding
4900     0x02, 0x00, 0x00, 0x00,
4901     // tag kRNON
4902     'R', 'N', 'O', 'N',
4903     // end offset 8
4904     0x08, 0x00, 0x00, 0x00,
4905     // tag kRSEQ
4906     'R', 'S', 'E', 'Q',
4907     // end offset 16
4908     0x10, 0x00, 0x00, 0x00,
4909     // nonce proof
4910     0x89, 0x67, 0x45, 0x23,
4911     0x01, 0xEF, 0xCD, 0xAB,
4912     // rejected sequence number
4913     0xBC, 0x9A, 0x78, 0x56,
4914     0x34, 0x12, 0x00, 0x00,
4915   };
4916
4917   scoped_ptr<QuicEncryptedPacket> data(
4918       framer_.BuildPublicResetPacket(reset_packet));
4919   ASSERT_TRUE(data != NULL);
4920
4921   test::CompareCharArraysWithHexError("constructed packet",
4922                                       data->data(), data->length(),
4923                                       AsChars(packet), arraysize(packet));
4924 }
4925
4926 TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
4927   QuicPublicResetPacket reset_packet;
4928   reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4929   reset_packet.public_header.reset_flag = true;
4930   reset_packet.public_header.version_flag = false;
4931   reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
4932   reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
4933   reset_packet.client_address = IPEndPoint(Loopback4(), 0x1234);
4934
4935   unsigned char packet[] = {
4936     // public flags (public reset, 8 byte ConnectionId)
4937     0x0E,
4938     // connection_id
4939     0x10, 0x32, 0x54, 0x76,
4940     0x98, 0xBA, 0xDC, 0xFE,
4941     // message tag (kPRST)
4942     'P', 'R', 'S', 'T',
4943     // num_entries (3) + padding
4944     0x03, 0x00, 0x00, 0x00,
4945     // tag kRNON
4946     'R', 'N', 'O', 'N',
4947     // end offset 8
4948     0x08, 0x00, 0x00, 0x00,
4949     // tag kRSEQ
4950     'R', 'S', 'E', 'Q',
4951     // end offset 16
4952     0x10, 0x00, 0x00, 0x00,
4953     // tag kCADR
4954     'C', 'A', 'D', 'R',
4955     // end offset 24
4956     0x18, 0x00, 0x00, 0x00,
4957     // nonce proof
4958     0x89, 0x67, 0x45, 0x23,
4959     0x01, 0xEF, 0xCD, 0xAB,
4960     // rejected sequence number
4961     0xBC, 0x9A, 0x78, 0x56,
4962     0x34, 0x12, 0x00, 0x00,
4963     // client address
4964     0x02, 0x00,
4965     0x7F, 0x00, 0x00, 0x01,
4966     0x34, 0x12,
4967   };
4968
4969   scoped_ptr<QuicEncryptedPacket> data(
4970       framer_.BuildPublicResetPacket(reset_packet));
4971   ASSERT_TRUE(data != NULL);
4972
4973   test::CompareCharArraysWithHexError("constructed packet",
4974                                       data->data(), data->length(),
4975                                       AsChars(packet), arraysize(packet));
4976 }
4977
4978 TEST_P(QuicFramerTest, BuildFecPacket) {
4979   QuicPacketHeader header;
4980   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
4981   header.public_header.reset_flag = false;
4982   header.public_header.version_flag = false;
4983   header.fec_flag = true;
4984   header.entropy_flag = true;
4985   header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
4986   header.is_in_fec_group = IN_FEC_GROUP;
4987   header.fec_group = GG_UINT64_C(0x123456789ABB);;
4988
4989   QuicFecData fec_data;
4990   fec_data.fec_group = 1;
4991   fec_data.redundancy = "abcdefghijklmnop";
4992
4993   unsigned char packet[] = {
4994     // public flags (8 byte connection_id)
4995     0x3C,
4996     // connection_id
4997     0x10, 0x32, 0x54, 0x76,
4998     0x98, 0xBA, 0xDC, 0xFE,
4999     // packet sequence number
5000     0xBC, 0x9A, 0x78, 0x56,
5001     0x34, 0x12,
5002     // private flags (entropy & fec group & fec packet)
5003     0x07,
5004     // first fec protected packet offset
5005     0x01,
5006
5007     // redundancy
5008     'a',  'b',  'c',  'd',
5009     'e',  'f',  'g',  'h',
5010     'i',  'j',  'k',  'l',
5011     'm',  'n',  'o',  'p',
5012   };
5013
5014   scoped_ptr<QuicPacket> data(
5015       framer_.BuildFecPacket(header, fec_data).packet);
5016   ASSERT_TRUE(data != NULL);
5017
5018   test::CompareCharArraysWithHexError("constructed packet",
5019                                       data->data(), data->length(),
5020                                       AsChars(packet), arraysize(packet));
5021 }
5022
5023 TEST_P(QuicFramerTest, EncryptPacket) {
5024   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
5025   unsigned char packet[] = {
5026     // public flags (8 byte connection_id)
5027     0x3C,
5028     // connection_id
5029     0x10, 0x32, 0x54, 0x76,
5030     0x98, 0xBA, 0xDC, 0xFE,
5031     // packet sequence number
5032     0xBC, 0x9A, 0x78, 0x56,
5033     0x34, 0x12,
5034     // private flags (fec group & fec packet)
5035     0x06,
5036     // first fec protected packet offset
5037     0x01,
5038
5039     // redundancy
5040     'a',  'b',  'c',  'd',
5041     'e',  'f',  'g',  'h',
5042     'i',  'j',  'k',  'l',
5043     'm',  'n',  'o',  'p',
5044   };
5045
5046   scoped_ptr<QuicPacket> raw(
5047       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
5048                                 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
5049                                 PACKET_6BYTE_SEQUENCE_NUMBER));
5050   scoped_ptr<QuicEncryptedPacket> encrypted(
5051       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
5052
5053   ASSERT_TRUE(encrypted.get() != NULL);
5054   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
5055 }
5056
5057 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
5058   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
5059   unsigned char packet[] = {
5060     // public flags (version, 8 byte connection_id)
5061     0x3D,
5062     // connection_id
5063     0x10, 0x32, 0x54, 0x76,
5064     0x98, 0xBA, 0xDC, 0xFE,
5065     // version tag
5066     'Q', '.', '1', '0',
5067     // packet sequence number
5068     0xBC, 0x9A, 0x78, 0x56,
5069     0x34, 0x12,
5070     // private flags (fec group & fec flags)
5071     0x06,
5072     // first fec protected packet offset
5073     0x01,
5074
5075     // redundancy
5076     'a',  'b',  'c',  'd',
5077     'e',  'f',  'g',  'h',
5078     'i',  'j',  'k',  'l',
5079     'm',  'n',  'o',  'p',
5080   };
5081
5082   scoped_ptr<QuicPacket> raw(
5083       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
5084                                 PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
5085                                 PACKET_6BYTE_SEQUENCE_NUMBER));
5086   scoped_ptr<QuicEncryptedPacket> encrypted(
5087       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
5088
5089   ASSERT_TRUE(encrypted.get() != NULL);
5090   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
5091 }
5092
5093 TEST_P(QuicFramerTest, Truncation) {
5094   if (framer_.version() <= QUIC_VERSION_15) {
5095     return;
5096   }
5097   QuicPacketHeader header;
5098   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
5099   header.public_header.reset_flag = false;
5100   header.public_header.version_flag = false;
5101   header.fec_flag = false;
5102   header.entropy_flag = false;
5103   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
5104   header.fec_group = 0;
5105
5106   QuicAckFrame ack_frame;
5107   ack_frame.received_info.largest_observed = 601;
5108   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; i += 2) {
5109     ack_frame.received_info.missing_packets.insert(i);
5110   }
5111
5112   // Create a packet with just the ack
5113   QuicFrame frame;
5114   frame.type = ACK_FRAME;
5115   frame.ack_frame = &ack_frame;
5116   QuicFrames frames;
5117   frames.push_back(frame);
5118
5119   scoped_ptr<QuicPacket> raw_ack_packet(
5120       framer_.BuildUnsizedDataPacket(header, frames).packet);
5121   ASSERT_TRUE(raw_ack_packet != NULL);
5122
5123   scoped_ptr<QuicEncryptedPacket> ack_packet(
5124       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
5125                             *raw_ack_packet));
5126
5127   // Now make sure we can turn our ack packet back into an ack frame
5128   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
5129   ASSERT_EQ(1u, visitor_.ack_frames_.size());
5130   const QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
5131   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
5132   EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
5133   ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
5134   SequenceNumberSet::const_iterator missing_iter =
5135       processed_ack_frame.received_info.missing_packets.begin();
5136   EXPECT_EQ(1u, *missing_iter);
5137   SequenceNumberSet::const_reverse_iterator last_missing_iter =
5138       processed_ack_frame.received_info.missing_packets.rbegin();
5139   EXPECT_EQ(509u, *last_missing_iter);
5140 }
5141
5142 TEST_P(QuicFramerTest, Truncation15) {
5143   if (framer_.version() > QUIC_VERSION_15) {
5144     return;
5145   }
5146   QuicPacketHeader header;
5147   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
5148   header.public_header.reset_flag = false;
5149   header.public_header.version_flag = false;
5150   header.fec_flag = false;
5151   header.entropy_flag = false;
5152   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
5153   header.fec_group = 0;
5154
5155   QuicAckFrame ack_frame;
5156   ack_frame.received_info.largest_observed = 601;
5157   ack_frame.sent_info.least_unacked = header.packet_sequence_number - 1;
5158   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; i += 2) {
5159     ack_frame.received_info.missing_packets.insert(i);
5160   }
5161
5162   // Create a packet with just the ack
5163   QuicFrame frame;
5164   frame.type = ACK_FRAME;
5165   frame.ack_frame = &ack_frame;
5166   QuicFrames frames;
5167   frames.push_back(frame);
5168
5169   scoped_ptr<QuicPacket> raw_ack_packet(
5170       framer_.BuildUnsizedDataPacket(header, frames).packet);
5171   ASSERT_TRUE(raw_ack_packet != NULL);
5172
5173   scoped_ptr<QuicEncryptedPacket> ack_packet(
5174       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
5175                             *raw_ack_packet));
5176
5177   // Now make sure we can turn our ack packet back into an ack frame
5178   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
5179   ASSERT_EQ(1u, visitor_.ack_frames_.size());
5180   const QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
5181   EXPECT_EQ(header.packet_sequence_number - 1,
5182             processed_ack_frame.sent_info.least_unacked);
5183   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
5184   EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
5185   ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
5186   SequenceNumberSet::const_iterator missing_iter =
5187       processed_ack_frame.received_info.missing_packets.begin();
5188   EXPECT_EQ(1u, *missing_iter);
5189   SequenceNumberSet::const_reverse_iterator last_missing_iter =
5190       processed_ack_frame.received_info.missing_packets.rbegin();
5191   EXPECT_EQ(509u, *last_missing_iter);
5192 }
5193
5194 TEST_P(QuicFramerTest, CleanTruncation) {
5195   QuicPacketHeader header;
5196   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
5197   header.public_header.reset_flag = false;
5198   header.public_header.version_flag = false;
5199   header.fec_flag = false;
5200   header.entropy_flag = true;
5201   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
5202   header.fec_group = 0;
5203
5204   QuicAckFrame ack_frame;
5205   ack_frame.received_info.largest_observed = 201;
5206   ack_frame.sent_info.least_unacked = header.packet_sequence_number - 2;
5207   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; ++i) {
5208     ack_frame.received_info.missing_packets.insert(i);
5209   }
5210
5211   // Create a packet with just the ack
5212   QuicFrame frame;
5213   frame.type = ACK_FRAME;
5214   frame.ack_frame = &ack_frame;
5215   QuicFrames frames;
5216   frames.push_back(frame);
5217
5218   scoped_ptr<QuicPacket> raw_ack_packet(
5219       framer_.BuildUnsizedDataPacket(header, frames).packet);
5220   ASSERT_TRUE(raw_ack_packet != NULL);
5221
5222   scoped_ptr<QuicEncryptedPacket> ack_packet(
5223       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
5224                             *raw_ack_packet));
5225
5226   // Now make sure we can turn our ack packet back into an ack frame
5227   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
5228
5229   // Test for clean truncation of the ack by comparing the length of the
5230   // original packets to the re-serialized packets.
5231   frames.clear();
5232   frame.type = ACK_FRAME;
5233   frame.ack_frame = visitor_.ack_frames_[0];
5234   frames.push_back(frame);
5235
5236   size_t original_raw_length = raw_ack_packet->length();
5237   raw_ack_packet.reset(
5238       framer_.BuildUnsizedDataPacket(header, frames).packet);
5239   ASSERT_TRUE(raw_ack_packet != NULL);
5240   EXPECT_EQ(original_raw_length, raw_ack_packet->length());
5241   ASSERT_TRUE(raw_ack_packet != NULL);
5242 }
5243
5244 TEST_P(QuicFramerTest, EntropyFlagTest) {
5245   unsigned char packet[] = {
5246     // public flags (8 byte connection_id)
5247     0x3C,
5248     // connection_id
5249     0x10, 0x32, 0x54, 0x76,
5250     0x98, 0xBA, 0xDC, 0xFE,
5251     // packet sequence number
5252     0xBC, 0x9A, 0x78, 0x56,
5253     0x34, 0x12,
5254     // private flags (Entropy)
5255     0x01,
5256
5257     // frame type (stream frame with fin and no length)
5258     0xDF,
5259     // stream id
5260     0x04, 0x03, 0x02, 0x01,
5261     // offset
5262     0x54, 0x76, 0x10, 0x32,
5263     0xDC, 0xFE, 0x98, 0xBA,
5264     // data
5265     'h',  'e',  'l',  'l',
5266     'o',  ' ',  'w',  'o',
5267     'r',  'l',  'd',  '!',
5268   };
5269
5270   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
5271   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5272   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
5273   ASSERT_TRUE(visitor_.header_.get());
5274   EXPECT_TRUE(visitor_.header_->entropy_flag);
5275   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
5276   EXPECT_FALSE(visitor_.header_->fec_flag);
5277 };
5278
5279 TEST_P(QuicFramerTest, FecEntropyTest) {
5280   unsigned char packet[] = {
5281     // public flags (8 byte connection_id)
5282     0x3C,
5283     // connection_id
5284     0x10, 0x32, 0x54, 0x76,
5285     0x98, 0xBA, 0xDC, 0xFE,
5286     // packet sequence number
5287     0xBC, 0x9A, 0x78, 0x56,
5288     0x34, 0x12,
5289     // private flags (Entropy & fec group & FEC)
5290     0x07,
5291     // first fec protected packet offset
5292     0xFF,
5293
5294     // frame type (stream frame with fin and no length)
5295     0xDF,
5296     // stream id
5297     0x04, 0x03, 0x02, 0x01,
5298     // offset
5299     0x54, 0x76, 0x10, 0x32,
5300     0xDC, 0xFE, 0x98, 0xBA,
5301     // data
5302     'h',  'e',  'l',  'l',
5303     'o',  ' ',  'w',  'o',
5304     'r',  'l',  'd',  '!',
5305   };
5306
5307   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
5308   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5309   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
5310   ASSERT_TRUE(visitor_.header_.get());
5311   EXPECT_TRUE(visitor_.header_->fec_flag);
5312   EXPECT_TRUE(visitor_.header_->entropy_flag);
5313   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
5314 };
5315
5316 TEST_P(QuicFramerTest, StopPacketProcessing) {
5317   unsigned char packet[] = {
5318     // public flags (8 byte connection_id)
5319     0x3C,
5320     // connection_id
5321     0x10, 0x32, 0x54, 0x76,
5322     0x98, 0xBA, 0xDC, 0xFE,
5323     // packet sequence number
5324     0xBC, 0x9A, 0x78, 0x56,
5325     0x34, 0x12,
5326     // Entropy
5327     0x01,
5328
5329     // frame type (stream frame with fin)
5330     0xFF,
5331     // stream id
5332     0x04, 0x03, 0x02, 0x01,
5333     // offset
5334     0x54, 0x76, 0x10, 0x32,
5335     0xDC, 0xFE, 0x98, 0xBA,
5336     // data length
5337     0x0c, 0x00,
5338     // data
5339     'h',  'e',  'l',  'l',
5340     'o',  ' ',  'w',  'o',
5341     'r',  'l',  'd',  '!',
5342
5343     // frame type (ack frame)
5344     0x40,
5345     // entropy hash of sent packets till least awaiting - 1.
5346     0x14,
5347     // least packet sequence number awaiting an ack
5348     0xA0, 0x9A, 0x78, 0x56,
5349     0x34, 0x12,
5350     // entropy hash of all received packets.
5351     0x43,
5352     // largest observed packet sequence number
5353     0xBF, 0x9A, 0x78, 0x56,
5354     0x34, 0x12,
5355     // num missing packets
5356     0x01,
5357     // missing packet
5358     0xBE, 0x9A, 0x78, 0x56,
5359     0x34, 0x12,
5360   };
5361
5362   MockFramerVisitor visitor;
5363   framer_.set_visitor(&visitor);
5364   EXPECT_CALL(visitor, OnPacket());
5365   EXPECT_CALL(visitor, OnPacketHeader(_));
5366   EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
5367   EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
5368   EXPECT_CALL(visitor, OnPacketComplete());
5369   EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
5370
5371   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
5372   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5373   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
5374 }
5375
5376 }  // namespace test
5377 }  // namespace net