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_protocol.h"
10 #include "net/quic/quic_sent_packet_manager.h"
11 #include "net/quic/quic_time.h"
12 #include "net/quic/quic_utils.h"
13 #include "net/quic/test_tools/quic_test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
22 class QuicConfigTest
: public ::testing::Test
{
25 config_
.SetDefaults();
26 config_
.set_initial_round_trip_time_us(kMaxInitialRoundTripTimeUs
, 0);
32 TEST_F(QuicConfigTest
, ToHandshakeMessage
) {
33 FLAGS_enable_quic_pacing
= false;
34 config_
.SetDefaults();
35 config_
.set_idle_connection_state_lifetime(QuicTime::Delta::FromSeconds(5),
36 QuicTime::Delta::FromSeconds(2));
37 config_
.set_max_streams_per_connection(4, 2);
38 config_
.set_peer_initial_flow_control_window_bytes(6);
39 CryptoHandshakeMessage msg
;
40 config_
.ToHandshakeMessage(&msg
);
43 QuicErrorCode error
= msg
.GetUint32(kICSL
, &value
);
44 EXPECT_EQ(QUIC_NO_ERROR
, error
);
47 error
= msg
.GetUint32(kMSPC
, &value
);
48 EXPECT_EQ(QUIC_NO_ERROR
, error
);
51 error
= msg
.GetUint32(kIFCW
, &value
);
52 EXPECT_EQ(QUIC_NO_ERROR
, error
);
57 error
= msg
.GetTaglist(kCGST
, &out
, &out_len
);
58 EXPECT_EQ(1u, out_len
);
59 EXPECT_EQ(kQBIC
, *out
);
62 TEST_F(QuicConfigTest
, ToHandshakeMessageWithPacing
) {
63 ValueRestore
<bool> old_flag(&FLAGS_enable_quic_pacing
, true);
65 config_
.SetDefaults();
66 CryptoHandshakeMessage msg
;
67 config_
.ToHandshakeMessage(&msg
);
71 EXPECT_EQ(QUIC_NO_ERROR
, msg
.GetTaglist(kCGST
, &out
, &out_len
));
72 EXPECT_EQ(2u, out_len
);
73 EXPECT_EQ(kPACE
, out
[0]);
74 EXPECT_EQ(kQBIC
, out
[1]);
77 TEST_F(QuicConfigTest
, ProcessClientHello
) {
78 QuicConfig client_config
;
80 cgst
.push_back(kINAR
);
81 cgst
.push_back(kQBIC
);
82 client_config
.set_congestion_control(cgst
, kQBIC
);
83 client_config
.set_idle_connection_state_lifetime(
84 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
),
85 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
));
86 client_config
.set_max_streams_per_connection(
87 2 * kDefaultMaxStreamsPerConnection
, kDefaultMaxStreamsPerConnection
);
88 client_config
.set_initial_round_trip_time_us(
89 10 * base::Time::kMicrosecondsPerMillisecond
,
90 10 * base::Time::kMicrosecondsPerMillisecond
);
91 const uint32 kFlowControlWindow
= 1234;
92 client_config
.set_peer_initial_flow_control_window_bytes(kFlowControlWindow
);
94 CryptoHandshakeMessage msg
;
95 client_config
.ToHandshakeMessage(&msg
);
97 const QuicErrorCode error
= config_
.ProcessClientHello(msg
, &error_details
);
98 EXPECT_EQ(QUIC_NO_ERROR
, error
);
99 EXPECT_TRUE(config_
.negotiated());
100 EXPECT_EQ(kQBIC
, config_
.congestion_control());
101 EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
),
102 config_
.idle_connection_state_lifetime());
103 EXPECT_EQ(kDefaultMaxStreamsPerConnection
,
104 config_
.max_streams_per_connection());
105 EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_
.keepalive_timeout());
106 EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond
,
107 config_
.initial_round_trip_time_us());
108 EXPECT_EQ(kFlowControlWindow
,
109 config_
.peer_initial_flow_control_window_bytes());
112 TEST_F(QuicConfigTest
, ProcessServerHello
) {
113 QuicConfig server_config
;
115 cgst
.push_back(kQBIC
);
116 server_config
.set_congestion_control(cgst
, kQBIC
);
117 server_config
.set_idle_connection_state_lifetime(
118 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2),
119 QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2));
120 server_config
.set_max_streams_per_connection(
121 kDefaultMaxStreamsPerConnection
/ 2,
122 kDefaultMaxStreamsPerConnection
/ 2);
123 server_config
.set_server_initial_congestion_window(kDefaultInitialWindow
/ 2,
124 kDefaultInitialWindow
/ 2);
125 server_config
.set_initial_round_trip_time_us(
126 10 * base::Time::kMicrosecondsPerMillisecond
,
127 10 * base::Time::kMicrosecondsPerMillisecond
);
128 const uint32 kFlowControlWindow
= 1234;
129 server_config
.set_peer_initial_flow_control_window_bytes(kFlowControlWindow
);
131 CryptoHandshakeMessage msg
;
132 server_config
.ToHandshakeMessage(&msg
);
133 string error_details
;
134 const QuicErrorCode error
= config_
.ProcessServerHello(msg
, &error_details
);
135 EXPECT_EQ(QUIC_NO_ERROR
, error
);
136 EXPECT_TRUE(config_
.negotiated());
137 EXPECT_EQ(kQBIC
, config_
.congestion_control());
138 EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs
/ 2),
139 config_
.idle_connection_state_lifetime());
140 EXPECT_EQ(kDefaultMaxStreamsPerConnection
/ 2,
141 config_
.max_streams_per_connection());
142 EXPECT_EQ(kDefaultInitialWindow
/ 2,
143 config_
.server_initial_congestion_window());
144 EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_
.keepalive_timeout());
145 EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond
,
146 config_
.initial_round_trip_time_us());
147 EXPECT_EQ(kFlowControlWindow
,
148 config_
.peer_initial_flow_control_window_bytes());
151 TEST_F(QuicConfigTest
, MissingOptionalValuesInCHLO
) {
152 CryptoHandshakeMessage msg
;
153 msg
.SetValue(kICSL
, 1);
154 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
156 // Set all REQUIRED tags.
157 msg
.SetValue(kICSL
, 1);
158 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
159 msg
.SetValue(kMSPC
, 1);
161 // No error, as rest are optional.
162 string error_details
;
163 const QuicErrorCode error
= config_
.ProcessClientHello(msg
, &error_details
);
164 EXPECT_EQ(QUIC_NO_ERROR
, error
);
166 EXPECT_EQ(0u, config_
.peer_initial_flow_control_window_bytes());
169 TEST_F(QuicConfigTest
, MissingOptionalValuesInSHLO
) {
170 CryptoHandshakeMessage msg
;
172 // Set all REQUIRED tags.
173 msg
.SetValue(kICSL
, 1);
174 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
175 msg
.SetValue(kMSPC
, 1);
177 // No error, as rest are optional.
178 string error_details
;
179 const QuicErrorCode error
= config_
.ProcessServerHello(msg
, &error_details
);
180 EXPECT_EQ(QUIC_NO_ERROR
, error
);
182 EXPECT_EQ(0u, config_
.peer_initial_flow_control_window_bytes());
185 TEST_F(QuicConfigTest
, MissingValueInCHLO
) {
186 CryptoHandshakeMessage msg
;
187 msg
.SetValue(kICSL
, 1);
188 msg
.SetVector(kCGST
, QuicTagVector(1, kQBIC
));
189 // Missing kMSPC. KATO is optional.
190 string error_details
;
191 const QuicErrorCode error
= config_
.ProcessClientHello(msg
, &error_details
);
192 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
, error
);
195 TEST_F(QuicConfigTest
, MissingValueInSHLO
) {
196 CryptoHandshakeMessage msg
;
197 msg
.SetValue(kICSL
, 1);
198 msg
.SetValue(kMSPC
, 3);
199 // Missing CGST. KATO is optional.
200 string error_details
;
201 const QuicErrorCode error
= config_
.ProcessServerHello(msg
, &error_details
);
202 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
, error
);
205 TEST_F(QuicConfigTest
, OutOfBoundSHLO
) {
206 QuicConfig server_config
;
207 server_config
.set_idle_connection_state_lifetime(
208 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
),
209 QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs
));
211 CryptoHandshakeMessage msg
;
212 server_config
.ToHandshakeMessage(&msg
);
213 string error_details
;
214 const QuicErrorCode error
= config_
.ProcessServerHello(msg
, &error_details
);
215 EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE
, error
);
218 TEST_F(QuicConfigTest
, MultipleNegotiatedValuesInVectorTag
) {
219 QuicConfig server_config
;
221 cgst
.push_back(kQBIC
);
222 cgst
.push_back(kINAR
);
223 server_config
.set_congestion_control(cgst
, kQBIC
);
225 CryptoHandshakeMessage msg
;
226 server_config
.ToHandshakeMessage(&msg
);
227 string error_details
;
228 const QuicErrorCode error
= config_
.ProcessServerHello(msg
, &error_details
);
229 EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE
, error
);
232 TEST_F(QuicConfigTest
, NoOverLapInCGST
) {
233 QuicConfig server_config
;
234 server_config
.SetDefaults();
236 cgst
.push_back(kINAR
);
237 server_config
.set_congestion_control(cgst
, kINAR
);
239 CryptoHandshakeMessage msg
;
240 string error_details
;
241 server_config
.ToHandshakeMessage(&msg
);
242 const QuicErrorCode error
= config_
.ProcessClientHello(msg
, &error_details
);
243 LOG(INFO
) << QuicUtils::ErrorToString(error
);
244 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
, error
);