Home | History | Annotate | Download | only in quic
      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_protocol.h"
      6 
      7 #include "base/stl_util.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace net {
     11 namespace test {
     12 namespace {
     13 
     14 TEST(QuicProtocolTest, AdjustErrorForVersion) {
     15   ASSERT_EQ(8, QUIC_STREAM_LAST_ERROR)
     16       << "Any additions to QuicRstStreamErrorCode require an addition to "
     17       << "AdjustErrorForVersion and this associated test.";
     18 
     19   EXPECT_EQ(QUIC_STREAM_NO_ERROR,
     20             AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING,
     21                                   QUIC_VERSION_17));
     22   EXPECT_EQ(QUIC_RST_FLOW_CONTROL_ACCOUNTING, AdjustErrorForVersion(
     23       QUIC_RST_FLOW_CONTROL_ACCOUNTING,
     24       static_cast<QuicVersion>(QUIC_VERSION_17 + 1)));
     25 }
     26 
     27 TEST(QuicProtocolTest, MakeQuicTag) {
     28   QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D');
     29   char bytes[4];
     30   memcpy(bytes, &tag, 4);
     31   EXPECT_EQ('A', bytes[0]);
     32   EXPECT_EQ('B', bytes[1]);
     33   EXPECT_EQ('C', bytes[2]);
     34   EXPECT_EQ('D', bytes[3]);
     35 }
     36 
     37 TEST(QuicProtocolTest, IsAawaitingPacket) {
     38   ReceivedPacketInfo received_info;
     39   received_info.largest_observed = 10u;
     40   EXPECT_TRUE(IsAwaitingPacket(received_info, 11u));
     41   EXPECT_FALSE(IsAwaitingPacket(received_info, 1u));
     42 
     43   received_info.missing_packets.insert(10);
     44   EXPECT_TRUE(IsAwaitingPacket(received_info, 10u));
     45 }
     46 
     47 TEST(QuicProtocolTest, InsertMissingPacketsBetween) {
     48   ReceivedPacketInfo received_info;
     49   InsertMissingPacketsBetween(&received_info, 4u, 10u);
     50   EXPECT_EQ(6u, received_info.missing_packets.size());
     51 
     52   QuicPacketSequenceNumber i = 4;
     53   for (SequenceNumberSet::iterator it = received_info.missing_packets.begin();
     54        it != received_info.missing_packets.end(); ++it, ++i) {
     55     EXPECT_EQ(i, *it);
     56   }
     57 }
     58 
     59 TEST(QuicProtocolTest, QuicVersionToQuicTag) {
     60   // If you add a new version to the QuicVersion enum you will need to add a new
     61   // case to QuicVersionToQuicTag, otherwise this test will fail.
     62 
     63   // TODO(rtenneti): Enable checking of Log(ERROR) messages.
     64 #if 0
     65   // Any logs would indicate an unsupported version which we don't expect.
     66   ScopedMockLog log(kDoNotCaptureLogsYet);
     67   EXPECT_CALL(log, Log(_, _, _)).Times(0);
     68   log.StartCapturingLogs();
     69 #endif
     70 
     71   // Explicitly test a specific version.
     72   EXPECT_EQ(MakeQuicTag('Q', '0', '1', '6'),
     73             QuicVersionToQuicTag(QUIC_VERSION_16));
     74 
     75   // Loop over all supported versions and make sure that we never hit the
     76   // default case (i.e. all supported versions should be successfully converted
     77   // to valid QuicTags).
     78   for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
     79     QuicVersion version = kSupportedQuicVersions[i];
     80     EXPECT_LT(0u, QuicVersionToQuicTag(version));
     81   }
     82 }
     83 
     84 TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) {
     85   // TODO(rtenneti): Enable checking of Log(ERROR) messages.
     86 #if 0
     87   // TODO(rjshade): Change to DFATAL once we actually support multiple versions,
     88   // and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use
     89   // mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED.
     90   ScopedMockLog log(kDoNotCaptureLogsYet);
     91   EXPECT_CALL(log, Log(ERROR, _, "Unsupported QuicVersion: 0")).Times(1);
     92   log.StartCapturingLogs();
     93 #endif
     94 
     95   EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED));
     96 }
     97 
     98 TEST(QuicProtocolTest, QuicTagToQuicVersion) {
     99   // If you add a new version to the QuicVersion enum you will need to add a new
    100   // case to QuicTagToQuicVersion, otherwise this test will fail.
    101 
    102   // TODO(rtenneti): Enable checking of Log(ERROR) messages.
    103 #if 0
    104   // Any logs would indicate an unsupported version which we don't expect.
    105   ScopedMockLog log(kDoNotCaptureLogsYet);
    106   EXPECT_CALL(log, Log(_, _, _)).Times(0);
    107   log.StartCapturingLogs();
    108 #endif
    109 
    110   // Explicitly test specific versions.
    111   EXPECT_EQ(QUIC_VERSION_16,
    112             QuicTagToQuicVersion(MakeQuicTag('Q', '0', '1', '6')));
    113 
    114   for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
    115     QuicVersion version = kSupportedQuicVersions[i];
    116 
    117     // Get the tag from the version (we can loop over QuicVersions easily).
    118     QuicTag tag = QuicVersionToQuicTag(version);
    119     EXPECT_LT(0u, tag);
    120 
    121     // Now try converting back.
    122     QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag);
    123     EXPECT_EQ(version, tag_to_quic_version);
    124     EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version);
    125   }
    126 }
    127 
    128 TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) {
    129   // TODO(rtenneti): Enable checking of Log(ERROR) messages.
    130 #if 0
    131   ScopedMockLog log(kDoNotCaptureLogsYet);
    132 #ifndef NDEBUG
    133   EXPECT_CALL(log, Log(INFO, _, "Unsupported QuicTag version: FAKE")).Times(1);
    134 #endif
    135   log.StartCapturingLogs();
    136 #endif
    137 
    138   EXPECT_EQ(QUIC_VERSION_UNSUPPORTED,
    139             QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
    140 }
    141 
    142 TEST(QuicProtocolTest, QuicVersionToString) {
    143   EXPECT_EQ("QUIC_VERSION_16", QuicVersionToString(QUIC_VERSION_16));
    144   EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
    145             QuicVersionToString(QUIC_VERSION_UNSUPPORTED));
    146 
    147   QuicVersion single_version[] = {QUIC_VERSION_16};
    148   QuicVersionVector versions_vector;
    149   for (size_t i = 0; i < arraysize(single_version); ++i) {
    150     versions_vector.push_back(single_version[i]);
    151   }
    152   EXPECT_EQ("QUIC_VERSION_16", QuicVersionVectorToString(versions_vector));
    153 
    154   QuicVersion multiple_versions[] = {QUIC_VERSION_UNSUPPORTED, QUIC_VERSION_16};
    155   versions_vector.clear();
    156   for (size_t i = 0; i < arraysize(multiple_versions); ++i) {
    157     versions_vector.push_back(multiple_versions[i]);
    158   }
    159   EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_16",
    160             QuicVersionVectorToString(versions_vector));
    161 
    162   // Make sure that all supported versions are present in QuicVersionToString.
    163   for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
    164     QuicVersion version = kSupportedQuicVersions[i];
    165     EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version));
    166   }
    167 }
    168 
    169 }  // namespace
    170 }  // namespace test
    171 }  // namespace net
    172