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
, MakeQuicTag
) {
15 QuicTag tag
= MakeQuicTag('A', 'B', 'C', 'D');
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]);
24 TEST(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));
30 received_info
.missing_packets
.insert(10);
31 EXPECT_TRUE(IsAwaitingPacket(received_info
, 10u));
34 TEST(QuicProtocolTest
, InsertMissingPacketsBetween
) {
35 ReceivedPacketInfo received_info
;
36 InsertMissingPacketsBetween(&received_info
, 4u, 10u);
37 EXPECT_EQ(6u, received_info
.missing_packets
.size());
39 QuicPacketSequenceNumber i
= 4;
40 for (SequenceNumberSet::iterator it
= received_info
.missing_packets
.begin();
41 it
!= received_info
.missing_packets
.end(); ++it
, ++i
) {
46 TEST(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.
50 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
58 // Explicitly test a specific version.
59 EXPECT_EQ(MakeQuicTag('Q', '0', '1', '2'),
60 QuicVersionToQuicTag(QUIC_VERSION_12
));
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 QuicVersion version
= kSupportedQuicVersions
[i
];
67 EXPECT_LT(0u, QuicVersionToQuicTag(version
));
71 TEST(QuicProtocolTest
, QuicVersionToQuicTagUnsupported
) {
72 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
82 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED
));
85 TEST(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.
89 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
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();
97 // Explicitly test specific versions.
98 EXPECT_EQ(QUIC_VERSION_12
,
99 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '1', '2')));
101 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
102 QuicVersion version
= kSupportedQuicVersions
[i
];
104 // Get the tag from the version (we can loop over QuicVersions easily).
105 QuicTag tag
= QuicVersionToQuicTag(version
);
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
);
115 TEST(QuicProtocolTest
, QuicTagToQuicVersionUnsupported
) {
116 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
118 ScopedMockLog
log(kDoNotCaptureLogsYet
);
120 EXPECT_CALL(log
, Log(INFO
, _
, "Unsupported QuicTag version: FAKE")).Times(1);
122 log
.StartCapturingLogs();
125 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED
,
126 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
129 TEST(QuicProtocolTest
, QuicVersionToString
) {
130 EXPECT_EQ("QUIC_VERSION_12", QuicVersionToString(QUIC_VERSION_12
));
131 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
132 QuicVersionToString(QUIC_VERSION_UNSUPPORTED
));
134 QuicVersion single_version
[] = {QUIC_VERSION_12
};
135 QuicVersionVector versions_vector
;
136 for (size_t i
= 0; i
< arraysize(single_version
); ++i
) {
137 versions_vector
.push_back(single_version
[i
]);
139 EXPECT_EQ("QUIC_VERSION_12", QuicVersionVectorToString(versions_vector
));
141 QuicVersion multiple_versions
[] = {QUIC_VERSION_UNSUPPORTED
, QUIC_VERSION_12
};
142 versions_vector
.clear();
143 for (size_t i
= 0; i
< arraysize(multiple_versions
); ++i
) {
144 versions_vector
.push_back(multiple_versions
[i
]);
146 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_12",
147 QuicVersionVectorToString(versions_vector
));
149 // Make sure that all supported versions are present in QuicVersionToString.
150 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
151 QuicVersion version
= kSupportedQuicVersions
[i
];
152 EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version
));