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