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