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