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