1 // Copyright (c) 2013 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_config.h"
7 #include "net/quic/crypto/crypto_handshake_message.h"
8 #include "net/quic/crypto/crypto_protocol.h"
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_protocol.h"
11 #include "net/quic/quic_sent_packet_manager.h"
12 #include "net/quic/quic_time.h"
13 #include "net/quic/quic_utils.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/test/gtest_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
24 class QuicConfigTest
: public ::testing::Test
{
27 config_
.SetDefaults();
33 TEST_F(QuicConfigTest
, ToHandshakeMessage
) {
34 ValueRestore
<bool> old_flag(&FLAGS_enable_quic_pacing
, false);
35 config_
.SetDefaults();
36 config_
.SetInitialFlowControlWindowToSend(
37 kInitialSessionFlowControlWindowForTest
);
38 config_
.SetInitialStreamFlowControlWindowToSend(
39 kInitialStreamFlowControlWindowForTest
);
40 config_
.SetInitialSessionFlowControlWindowToSend(
41 kInitialSessionFlowControlWindowForTest
);
42 config_
.set_idle_connection_state_lifetime(QuicTime::Delta::FromSeconds(5),
43 QuicTime::Delta::FromSeconds(2));
44 config_
.set_max_streams_per_connection(4, 2);
45 config_
.SetSocketReceiveBufferToSend(kDefaultSocketReceiveBuffer
);
46 CryptoHandshakeMessage msg
;
47 config_
.ToHandshakeMessage(&msg
);
50 QuicErrorCode error
= msg
.GetUint32(kICSL
, &value
);
51 EXPECT_EQ(QUIC_NO_ERROR
, error
);
54 error
= msg
.GetUint32(kMSPC
, &value
);
55 EXPECT_EQ(QUIC_NO_ERROR
, error
);
58 error
= msg
.GetUint32(kIFCW
, &value
);
59 EXPECT_EQ(QUIC_NO_ERROR
, error
);
60 EXPECT_EQ(kInitialSessionFlowControlWindowForTest
, value
);
62 error
= msg
.GetUint32(kSFCW
, &value
);
63 EXPECT_EQ(QUIC_NO_ERROR
, error
);
64 EXPECT_EQ(kInitialStreamFlowControlWindowForTest
, value
);
66 error
= msg
.GetUint32(kCFCW
, &value
);
67 EXPECT_EQ(QUIC_NO_ERROR
, error
);
68 EXPECT_EQ(kInitialSessionFlowControlWindowForTest
, value
);
70 error
= msg
.GetUint32(kSRBF
, &value
);
71 EXPECT_EQ(QUIC_NO_ERROR
, error
);
72 EXPECT_EQ(kDefaultSocketReceiveBuffer
, value
);
76 error
= msg
.GetTaglist(kCGST
, &out
, &out_len
);
77 EXPECT_EQ(1u, out_len
);
78 EXPECT_EQ(kQBIC
, *out
);
81 TEST_F(QuicConfigTest
, ToHandshakeMessageWithPacing
) {
82 ValueRestore
<bool> old_flag(&FLAGS_enable_quic_pacing
, true);
84 config_
.SetDefaults();
85 CryptoHandshakeMessage msg
;
86 config_
.ToHandshakeMessage(&msg
);
90 EXPECT_EQ(QUIC_NO_ERROR
, msg
.GetTaglist(kCGST
, &out
, &out_len
));
91 EXPECT_EQ(2u, out_len
);
92 EXPECT_EQ(kPACE
, out
[0]);
93 EXPECT_EQ(kQBIC
, out
[1]);
96 TEST_F(QuicConfigTest
, ProcessClientHello
) {
97 QuicConfig client_config
;
99 cgst
.push_back(kTSTP
);
100 cgst
.push_back(kQBIC
);
101 client_config
.set_congestion_feedback(cgst
, kQBIC
);
102 client_config
.set_idle_connection_state_lifetime(
103 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
),
104 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
));
105 client_config
.set_max_streams_per_connection(
106 2 * kDefaultMaxStreamsPerConnection
, kDefaultMaxStreamsPerConnection
);
107 client_config
.SetInitialRoundTripTimeUsToSend(
108 10 * base::Time::kMicrosecondsPerMillisecond
);
109 client_config
.SetInitialFlowControlWindowToSend(
110 2 * kInitialSessionFlowControlWindowForTest
);
111 client_config
.SetInitialStreamFlowControlWindowToSend(
112 2 * kInitialStreamFlowControlWindowForTest
);
113 client_config
.SetInitialSessionFlowControlWindowToSend(
114 2 * kInitialSessionFlowControlWindowForTest
);
115 client_config
.SetSocketReceiveBufferToSend(kDefaultSocketReceiveBuffer
);
117 copt
.push_back(kTBBR
);
118 copt
.push_back(kFHDR
);
119 client_config
.SetConnectionOptionsToSend(copt
);
120 CryptoHandshakeMessage msg
;
121 client_config
.ToHandshakeMessage(&msg
);
122 string error_details
;
123 const QuicErrorCode error
=
124 config_
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
125 EXPECT_EQ(QUIC_NO_ERROR
, error
);
126 EXPECT_TRUE(config_
.negotiated());
127 EXPECT_EQ(kQBIC
, config_
.congestion_feedback());
128 EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
),
129 config_
.idle_connection_state_lifetime());
130 EXPECT_EQ(kDefaultMaxStreamsPerConnection
,
131 config_
.max_streams_per_connection());
132 EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_
.keepalive_timeout());
133 EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond
,
134 config_
.ReceivedInitialRoundTripTimeUs());
135 EXPECT_FALSE(config_
.HasReceivedLossDetection());
136 EXPECT_TRUE(config_
.HasReceivedConnectionOptions());
137 EXPECT_EQ(2u, config_
.ReceivedConnectionOptions().size());
138 EXPECT_EQ(config_
.ReceivedConnectionOptions()[0], kTBBR
);
139 EXPECT_EQ(config_
.ReceivedConnectionOptions()[1], kFHDR
);
140 EXPECT_EQ(config_
.ReceivedInitialFlowControlWindowBytes(),
141 2 * kInitialSessionFlowControlWindowForTest
);
142 EXPECT_EQ(config_
.ReceivedInitialStreamFlowControlWindowBytes(),
143 2 * kInitialStreamFlowControlWindowForTest
);
144 EXPECT_EQ(config_
.ReceivedInitialSessionFlowControlWindowBytes(),
145 2 * kInitialSessionFlowControlWindowForTest
);
146 EXPECT_EQ(config_
.ReceivedSocketReceiveBuffer(),
147 kDefaultSocketReceiveBuffer
);
150 TEST_F(QuicConfigTest
, ProcessServerHello
) {
151 QuicConfig server_config
;
153 cgst
.push_back(kQBIC
);
154 server_config
.set_congestion_feedback(cgst
, kQBIC
);
155 server_config
.set_idle_connection_state_lifetime(
156 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2),
157 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2));
158 server_config
.set_max_streams_per_connection(
159 kDefaultMaxStreamsPerConnection
/ 2,
160 kDefaultMaxStreamsPerConnection
/ 2);
161 server_config
.SetInitialCongestionWindowToSend(kDefaultInitialWindow
/ 2);
162 server_config
.SetInitialRoundTripTimeUsToSend(
163 10 * base::Time::kMicrosecondsPerMillisecond
);
164 server_config
.SetInitialFlowControlWindowToSend(
165 2 * kInitialSessionFlowControlWindowForTest
);
166 server_config
.SetInitialStreamFlowControlWindowToSend(
167 2 * kInitialStreamFlowControlWindowForTest
);
168 server_config
.SetInitialSessionFlowControlWindowToSend(
169 2 * kInitialSessionFlowControlWindowForTest
);
170 server_config
.SetSocketReceiveBufferToSend(kDefaultSocketReceiveBuffer
);
171 CryptoHandshakeMessage msg
;
172 server_config
.ToHandshakeMessage(&msg
);
173 string error_details
;
174 const QuicErrorCode error
=
175 config_
.ProcessPeerHello(msg
, SERVER
, &error_details
);
176 EXPECT_EQ(QUIC_NO_ERROR
, error
);
177 EXPECT_TRUE(config_
.negotiated());
178 EXPECT_EQ(kQBIC
, config_
.congestion_feedback());
179 EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2),
180 config_
.idle_connection_state_lifetime());
181 EXPECT_EQ(kDefaultMaxStreamsPerConnection
/ 2,
182 config_
.max_streams_per_connection());
183 EXPECT_EQ(kDefaultInitialWindow
/ 2,
184 config_
.ReceivedInitialCongestionWindow());
185 EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_
.keepalive_timeout());
186 EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond
,
187 config_
.ReceivedInitialRoundTripTimeUs());
188 EXPECT_FALSE(config_
.HasReceivedLossDetection());
189 EXPECT_EQ(config_
.ReceivedInitialFlowControlWindowBytes(),
190 2 * kInitialSessionFlowControlWindowForTest
);
191 EXPECT_EQ(config_
.ReceivedInitialStreamFlowControlWindowBytes(),
192 2 * kInitialStreamFlowControlWindowForTest
);
193 EXPECT_EQ(config_
.ReceivedInitialSessionFlowControlWindowBytes(),
194 2 * kInitialSessionFlowControlWindowForTest
);
195 EXPECT_EQ(config_
.ReceivedSocketReceiveBuffer(),
196 kDefaultSocketReceiveBuffer
);
199 TEST_F(QuicConfigTest
, MissingOptionalValuesInCHLO
) {
200 CryptoHandshakeMessage msg
;
201 msg
.SetValue(kICSL
, 1);
202 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
204 // Set all REQUIRED tags.
205 msg
.SetValue(kICSL
, 1);
206 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
207 msg
.SetValue(kMSPC
, 1);
209 // No error, as rest are optional.
210 string error_details
;
211 const QuicErrorCode error
=
212 config_
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
213 EXPECT_EQ(QUIC_NO_ERROR
, error
);
215 EXPECT_FALSE(config_
.HasReceivedInitialFlowControlWindowBytes());
218 TEST_F(QuicConfigTest
, MissingOptionalValuesInSHLO
) {
219 CryptoHandshakeMessage msg
;
221 // Set all REQUIRED tags.
222 msg
.SetValue(kICSL
, 1);
223 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
224 msg
.SetValue(kMSPC
, 1);
226 // No error, as rest are optional.
227 string error_details
;
228 const QuicErrorCode error
=
229 config_
.ProcessPeerHello(msg
, SERVER
, &error_details
);
230 EXPECT_EQ(QUIC_NO_ERROR
, error
);
232 EXPECT_FALSE(config_
.HasReceivedInitialFlowControlWindowBytes());
235 TEST_F(QuicConfigTest
, MissingValueInCHLO
) {
236 CryptoHandshakeMessage msg
;
237 msg
.SetValue(kICSL
, 1);
238 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
239 // Missing kMSPC. KATO is optional.
240 string error_details
;
241 const QuicErrorCode error
=
242 config_
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
243 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
, error
);
246 TEST_F(QuicConfigTest
, MissingValueInSHLO
) {
247 CryptoHandshakeMessage msg
;
248 msg
.SetValue(kICSL
, 1);
249 msg
.SetValue(kMSPC
, 3);
250 // Missing CGST. KATO is optional.
251 string error_details
;
252 const QuicErrorCode error
=
253 config_
.ProcessPeerHello(msg
, SERVER
, &error_details
);
254 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
, error
);
257 TEST_F(QuicConfigTest
, OutOfBoundSHLO
) {
258 QuicConfig server_config
;
259 server_config
.set_idle_connection_state_lifetime(
260 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
),
261 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
));
263 CryptoHandshakeMessage msg
;
264 server_config
.ToHandshakeMessage(&msg
);
265 string error_details
;
266 const QuicErrorCode error
=
267 config_
.ProcessPeerHello(msg
, SERVER
, &error_details
);
268 EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE
, error
);
271 TEST_F(QuicConfigTest
, MultipleNegotiatedValuesInVectorTag
) {
272 QuicConfig server_config
;
274 cgst
.push_back(kQBIC
);
275 cgst
.push_back(kTSTP
);
276 server_config
.set_congestion_feedback(cgst
, kQBIC
);
278 CryptoHandshakeMessage msg
;
279 server_config
.ToHandshakeMessage(&msg
);
280 string error_details
;
281 const QuicErrorCode error
=
282 config_
.ProcessPeerHello(msg
, SERVER
, &error_details
);
283 EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE
, error
);
286 TEST_F(QuicConfigTest
, NoOverLapInCGST
) {
287 QuicConfig server_config
;
288 server_config
.SetDefaults();
290 cgst
.push_back(kTSTP
);
291 server_config
.set_congestion_feedback(cgst
, kTSTP
);
293 CryptoHandshakeMessage msg
;
294 string error_details
;
295 server_config
.ToHandshakeMessage(&msg
);
296 const QuicErrorCode error
=
297 config_
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
298 DVLOG(1) << QuicUtils::ErrorToString(error
);
299 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
, error
);
302 TEST_F(QuicConfigTest
, InvalidFlowControlWindow
) {
303 // QuicConfig should not accept an invalid flow control window to send to the
304 // peer: the receive window must be at least the default of 16 Kb.
306 const uint64 kInvalidWindow
= kDefaultFlowControlSendWindow
- 1;
307 EXPECT_DFATAL(config
.SetInitialFlowControlWindowToSend(kInvalidWindow
),
308 "Initial flow control receive window");
310 EXPECT_EQ(kDefaultFlowControlSendWindow
,
311 config
.GetInitialFlowControlWindowToSend());