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.
5 #include "net/quic/quic_protocol.h"
7 #include "base/stl_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
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.";
19 // If we ever add different RST codes, we should have a test akin to the
21 // EXPECT_EQ(QUIC_RST_ACKNOWLEDGEMENT, AdjustErrorForVersion(
22 // QUIC_RST_ACKNOWLEDGEMENT,
26 TEST(QuicProtocolTest
, MakeQuicTag
) {
27 QuicTag tag
= MakeQuicTag('A', 'B', 'C', 'D');
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]);
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));
42 ack_frame
.missing_packets
.insert(10);
43 EXPECT_TRUE(IsAwaitingPacket(ack_frame
, 10u));
46 TEST(QuicProtocolTest
, InsertMissingPacketsBetween
) {
47 QuicAckFrame ack_frame
;
48 InsertMissingPacketsBetween(&ack_frame
, 4u, 10u);
49 EXPECT_EQ(6u, ack_frame
.missing_packets
.size());
51 QuicPacketNumber i
= 4;
52 for (PacketNumberSet::iterator it
= ack_frame
.missing_packets
.begin();
53 it
!= ack_frame
.missing_packets
.end(); ++it
, ++i
) {
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.
62 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
70 // Explicitly test a specific version.
71 EXPECT_EQ(MakeQuicTag('Q', '0', '2', '5'),
72 QuicVersionToQuicTag(QUIC_VERSION_25
));
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
));
83 TEST(QuicProtocolTest
, QuicVersionToQuicTagUnsupported
) {
84 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
94 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED
));
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.
101 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
109 // Explicitly test specific versions.
110 EXPECT_EQ(QUIC_VERSION_25
,
111 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '2', '5')));
113 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
114 QuicVersion version
= kSupportedQuicVersions
[i
];
116 // Get the tag from the version (we can loop over QuicVersions easily).
117 QuicTag tag
= QuicVersionToQuicTag(version
);
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
);
127 TEST(QuicProtocolTest
, QuicTagToQuicVersionUnsupported
) {
128 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
130 ScopedMockLog
log(kDoNotCaptureLogsYet
);
132 EXPECT_CALL(log
, Log(INFO
, _
, "Unsupported QuicTag version: FAKE")).Times(1);
134 log
.StartCapturingLogs();
137 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED
,
138 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
141 TEST(QuicProtocolTest
, QuicVersionToString
) {
142 EXPECT_EQ("QUIC_VERSION_25", QuicVersionToString(QUIC_VERSION_25
));
143 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
144 QuicVersionToString(QUIC_VERSION_UNSUPPORTED
));
146 QuicVersion single_version
[] = {QUIC_VERSION_25
};
147 QuicVersionVector versions_vector
;
148 for (size_t i
= 0; i
< arraysize(single_version
); ++i
) {
149 versions_vector
.push_back(single_version
[i
]);
151 EXPECT_EQ("QUIC_VERSION_25", QuicVersionVectorToString(versions_vector
));
153 QuicVersion multiple_versions
[] = {QUIC_VERSION_UNSUPPORTED
, QUIC_VERSION_25
};
154 versions_vector
.clear();
155 for (size_t i
= 0; i
< arraysize(multiple_versions
); ++i
) {
156 versions_vector
.push_back(multiple_versions
[i
]);
158 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_25",
159 QuicVersionVectorToString(versions_vector
));
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
));