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