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