1 // Copyright 2014 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 "base/command_line.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/test/mock_entropy_provider.h"
8 #include "chrome/browser/io_thread.h"
9 #include "chrome/common/chrome_switches.h"
10 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
11 #include "net/http/http_network_session.h"
12 #include "net/http/http_server_properties_impl.h"
13 #include "net/quic/quic_protocol.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::ElementsAre
;
23 static void ConfigureQuicGlobals(
24 const base::CommandLine
& command_line
,
25 base::StringPiece quic_trial_group
,
26 const std::map
<std::string
, std::string
>& quic_trial_params
,
27 bool is_quic_allowed_by_policy
,
28 IOThread::Globals
* globals
) {
29 IOThread::ConfigureQuicGlobals(command_line
, quic_trial_group
,
30 quic_trial_params
, is_quic_allowed_by_policy
,
34 static void ConfigureSpdyGlobals(
35 const base::CommandLine
& command_line
,
36 base::StringPiece spdy_trial_group
,
37 const std::map
<std::string
, std::string
>& spdy_trial_params
,
38 IOThread::Globals
* globals
) {
39 IOThread::ConfigureSpdyGlobals(command_line
, spdy_trial_group
,
40 spdy_trial_params
, globals
);
43 static void InitializeNetworkSessionParamsFromGlobals(
44 const IOThread::Globals
& globals
,
45 net::HttpNetworkSession::Params
* params
) {
46 IOThread::InitializeNetworkSessionParamsFromGlobals(globals
, params
);
50 class IOThreadTest
: public testing::Test
{
53 : command_line_(base::CommandLine::NO_PROGRAM
),
54 is_quic_allowed_by_policy_(true) {
55 globals_
.http_server_properties
.reset(new net::HttpServerPropertiesImpl());
58 void ConfigureQuicGlobals() {
59 IOThreadPeer::ConfigureQuicGlobals(command_line_
,
62 is_quic_allowed_by_policy_
,
66 void ConfigureSpdyGlobals() {
67 IOThreadPeer::ConfigureSpdyGlobals(command_line_
, field_trial_group_
,
68 field_trial_params_
, &globals_
);
71 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params
* params
) {
72 IOThreadPeer::InitializeNetworkSessionParamsFromGlobals(globals_
, params
);
75 base::CommandLine command_line_
;
76 IOThread::Globals globals_
;
77 std::string field_trial_group_
;
78 bool is_quic_allowed_by_policy_
;
79 std::map
<std::string
, std::string
> field_trial_params_
;
82 TEST_F(IOThreadTest
, InitializeNetworkSessionParamsFromGlobals
) {
83 globals_
.quic_connection_options
.push_back(net::kTBBR
);
84 globals_
.quic_connection_options
.push_back(net::kTIME
);
86 net::HttpNetworkSession::Params params
;
87 InitializeNetworkSessionParams(¶ms
);
88 EXPECT_EQ(globals_
.quic_connection_options
,
89 params
.quic_connection_options
);
92 TEST_F(IOThreadTest
, SpdyFieldTrialHoldbackEnabled
) {
93 net::HttpStreamFactory::set_spdy_enabled(true);
94 field_trial_group_
= "SpdyDisabled";
95 ConfigureSpdyGlobals();
96 EXPECT_FALSE(net::HttpStreamFactory::spdy_enabled());
99 TEST_F(IOThreadTest
, SpdyFieldTrialSpdy31Enabled
) {
100 field_trial_group_
= "Spdy31Enabled";
101 ConfigureSpdyGlobals();
102 EXPECT_THAT(globals_
.next_protos
,
103 ElementsAre(net::kProtoHTTP11
,
107 TEST_F(IOThreadTest
, SpdyFieldTrialSpdy4Enabled
) {
108 field_trial_group_
= "Spdy4Enabled";
109 ConfigureSpdyGlobals();
111 globals_
.next_protos
,
112 ElementsAre(net::kProtoHTTP11
, net::kProtoSPDY31
, net::kProtoHTTP2
));
115 TEST_F(IOThreadTest
, SpdyFieldTrialDefault
) {
116 field_trial_group_
= "";
117 ConfigureSpdyGlobals();
119 globals_
.next_protos
,
120 ElementsAre(net::kProtoHTTP11
, net::kProtoSPDY31
, net::kProtoHTTP2
));
123 TEST_F(IOThreadTest
, SpdyFieldTrialParametrized
) {
124 field_trial_params_
["enable_spdy31"] = "false";
125 // Undefined parameter "enable_http2_14" should default to false.
126 field_trial_params_
["enable_http2"] = "true";
127 field_trial_group_
= "ParametrizedHTTP2Only";
128 ConfigureSpdyGlobals();
129 EXPECT_THAT(globals_
.next_protos
,
130 ElementsAre(net::kProtoHTTP11
, net::kProtoHTTP2
));
133 TEST_F(IOThreadTest
, SpdyCommandLineUseSpdyOff
) {
134 command_line_
.AppendSwitchASCII("use-spdy", "off");
135 // Command line should overwrite field trial group.
136 field_trial_group_
= "Spdy4Enabled";
137 ConfigureSpdyGlobals();
138 EXPECT_EQ(0u, globals_
.next_protos
.size());
141 TEST_F(IOThreadTest
, DisableQuicByDefault
) {
142 ConfigureQuicGlobals();
143 net::HttpNetworkSession::Params params
;
144 InitializeNetworkSessionParams(¶ms
);
145 EXPECT_FALSE(params
.enable_quic
);
146 EXPECT_FALSE(params
.enable_quic_for_proxies
);
147 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
150 TEST_F(IOThreadTest
, EnableQuicFromFieldTrialGroup
) {
151 field_trial_group_
= "Enabled";
153 ConfigureQuicGlobals();
154 net::HttpNetworkSession::Params default_params
;
155 net::HttpNetworkSession::Params params
;
156 InitializeNetworkSessionParams(¶ms
);
157 EXPECT_TRUE(params
.enable_quic
);
158 EXPECT_FALSE(params
.enable_insecure_quic
);
159 EXPECT_TRUE(params
.enable_quic_for_proxies
);
160 EXPECT_EQ(1350u, params
.quic_max_packet_length
);
161 EXPECT_EQ(1.0, params
.alternative_service_probability_threshold
);
162 EXPECT_EQ(default_params
.quic_supported_versions
,
163 params
.quic_supported_versions
);
164 EXPECT_EQ(net::QuicTagVector(), params
.quic_connection_options
);
165 EXPECT_FALSE(params
.quic_always_require_handshake_confirmation
);
166 EXPECT_FALSE(params
.quic_disable_connection_pooling
);
167 EXPECT_EQ(0.25f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
168 EXPECT_FALSE(params
.quic_enable_connection_racing
);
169 EXPECT_FALSE(params
.quic_enable_non_blocking_io
);
170 EXPECT_FALSE(params
.quic_disable_disk_cache
);
171 EXPECT_FALSE(params
.quic_prefer_aes
);
172 EXPECT_FALSE(params
.use_alternative_services
);
173 EXPECT_EQ(4, params
.quic_max_number_of_lossy_connections
);
174 EXPECT_EQ(0.5f
, params
.quic_packet_loss_threshold
);
175 EXPECT_FALSE(params
.quic_delay_tcp_race
);
176 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
179 TEST_F(IOThreadTest
, EnableQuicFromQuicProxyFieldTrialGroup
) {
180 base::FieldTrialList
field_trial_list(new base::MockEntropyProvider());
181 base::FieldTrialList::CreateFieldTrial(
182 data_reduction_proxy::params::GetQuicFieldTrialName(), "Enabled");
184 ConfigureQuicGlobals();
185 net::HttpNetworkSession::Params params
;
186 InitializeNetworkSessionParams(¶ms
);
187 EXPECT_FALSE(params
.enable_quic
);
188 EXPECT_TRUE(params
.enable_quic_for_proxies
);
189 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
190 EXPECT_EQ(1024 * 1024, params
.quic_socket_receive_buffer_size
);
193 TEST_F(IOThreadTest
, EnableQuicFromCommandLine
) {
194 command_line_
.AppendSwitch("enable-quic");
196 ConfigureQuicGlobals();
197 net::HttpNetworkSession::Params params
;
198 InitializeNetworkSessionParams(¶ms
);
199 EXPECT_TRUE(params
.enable_quic
);
200 EXPECT_TRUE(params
.enable_quic_for_proxies
);
201 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
204 TEST_F(IOThreadTest
, EnableAlternativeServicesFromCommandLineWithQuicDisabled
) {
205 command_line_
.AppendSwitch("enable-alternative-services");
207 ConfigureQuicGlobals();
208 net::HttpNetworkSession::Params params
;
209 InitializeNetworkSessionParams(¶ms
);
210 EXPECT_FALSE(params
.enable_quic
);
211 EXPECT_TRUE(params
.use_alternative_services
);
214 TEST_F(IOThreadTest
, EnableAlternativeServicesFromCommandLineWithQuicEnabled
) {
215 command_line_
.AppendSwitch("enable-quic");
216 command_line_
.AppendSwitch("enable-alternative-services");
218 ConfigureQuicGlobals();
219 net::HttpNetworkSession::Params params
;
220 InitializeNetworkSessionParams(¶ms
);
221 EXPECT_TRUE(params
.enable_quic
);
222 EXPECT_TRUE(params
.use_alternative_services
);
225 TEST_F(IOThreadTest
, EnableInsecureQuicFromFieldTrialParams
) {
226 field_trial_group_
= "Enabled";
227 field_trial_params_
["enable_insecure_quic"] = "true";
229 ConfigureQuicGlobals();
230 net::HttpNetworkSession::Params params
;
231 InitializeNetworkSessionParams(¶ms
);
232 EXPECT_TRUE(params
.enable_insecure_quic
);
235 TEST_F(IOThreadTest
, EnableInsecureQuicCommandLine
) {
236 command_line_
.AppendSwitch("enable-quic");
237 command_line_
.AppendSwitch("enable-insecure-quic");
239 ConfigureQuicGlobals();
240 net::HttpNetworkSession::Params params
;
241 InitializeNetworkSessionParams(¶ms
);
242 EXPECT_TRUE(params
.enable_insecure_quic
);
245 TEST_F(IOThreadTest
, PacketLengthFromCommandLine
) {
246 command_line_
.AppendSwitch("enable-quic");
247 command_line_
.AppendSwitchASCII("quic-max-packet-length", "1450");
249 ConfigureQuicGlobals();
250 net::HttpNetworkSession::Params params
;
251 InitializeNetworkSessionParams(¶ms
);
252 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
255 TEST_F(IOThreadTest
, PacketLengthFromFieldTrialParams
) {
256 field_trial_group_
= "Enabled";
257 field_trial_params_
["max_packet_length"] = "1450";
259 ConfigureQuicGlobals();
260 net::HttpNetworkSession::Params params
;
261 InitializeNetworkSessionParams(¶ms
);
262 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
265 TEST_F(IOThreadTest
, QuicVersionFromCommandLine
) {
266 command_line_
.AppendSwitch("enable-quic");
267 std::string version
=
268 net::QuicVersionToString(net::QuicSupportedVersions().back());
269 command_line_
.AppendSwitchASCII("quic-version", version
);
271 ConfigureQuicGlobals();
272 net::HttpNetworkSession::Params params
;
273 InitializeNetworkSessionParams(¶ms
);
274 net::QuicVersionVector supported_versions
;
275 supported_versions
.push_back(net::QuicSupportedVersions().back());
276 EXPECT_EQ(supported_versions
, params
.quic_supported_versions
);
279 TEST_F(IOThreadTest
, QuicVersionFromFieldTrialParams
) {
280 field_trial_group_
= "Enabled";
281 field_trial_params_
["quic_version"] =
282 net::QuicVersionToString(net::QuicSupportedVersions().back());
284 ConfigureQuicGlobals();
285 net::HttpNetworkSession::Params params
;
286 InitializeNetworkSessionParams(¶ms
);
287 net::QuicVersionVector supported_versions
;
288 supported_versions
.push_back(net::QuicSupportedVersions().back());
289 EXPECT_EQ(supported_versions
, params
.quic_supported_versions
);
292 TEST_F(IOThreadTest
, QuicConnectionOptionsFromCommandLine
) {
293 command_line_
.AppendSwitch("enable-quic");
294 command_line_
.AppendSwitchASCII("quic-connection-options",
297 ConfigureQuicGlobals();
298 net::HttpNetworkSession::Params params
;
299 InitializeNetworkSessionParams(¶ms
);
301 net::QuicTagVector options
;
302 options
.push_back(net::kTIME
);
303 options
.push_back(net::kTBBR
);
304 options
.push_back(net::kREJ
);
305 EXPECT_EQ(options
, params
.quic_connection_options
);
308 TEST_F(IOThreadTest
, QuicConnectionOptionsFromFieldTrialParams
) {
309 field_trial_group_
= "Enabled";
310 field_trial_params_
["connection_options"] = "TIME,TBBR,REJ";
312 ConfigureQuicGlobals();
313 net::HttpNetworkSession::Params params
;
314 InitializeNetworkSessionParams(¶ms
);
316 net::QuicTagVector options
;
317 options
.push_back(net::kTIME
);
318 options
.push_back(net::kTBBR
);
319 options
.push_back(net::kREJ
);
320 EXPECT_EQ(options
, params
.quic_connection_options
);
324 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams
) {
325 field_trial_group_
= "Enabled";
326 field_trial_params_
["always_require_handshake_confirmation"] = "true";
327 ConfigureQuicGlobals();
328 net::HttpNetworkSession::Params params
;
329 InitializeNetworkSessionParams(¶ms
);
330 EXPECT_TRUE(params
.quic_always_require_handshake_confirmation
);
334 QuicDisableConnectionPoolingFromFieldTrialParams
) {
335 field_trial_group_
= "Enabled";
336 field_trial_params_
["disable_connection_pooling"] = "true";
337 ConfigureQuicGlobals();
338 net::HttpNetworkSession::Params params
;
339 InitializeNetworkSessionParams(¶ms
);
340 EXPECT_TRUE(params
.quic_disable_connection_pooling
);
343 TEST_F(IOThreadTest
, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams
) {
344 field_trial_group_
= "Enabled";
345 field_trial_params_
["load_server_info_time_to_srtt"] = "0.5";
346 ConfigureQuicGlobals();
347 net::HttpNetworkSession::Params params
;
348 InitializeNetworkSessionParams(¶ms
);
349 EXPECT_EQ(0.5f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
352 TEST_F(IOThreadTest
, QuicEnableConnectionRacing
) {
353 field_trial_group_
= "Enabled";
354 field_trial_params_
["enable_connection_racing"] = "true";
355 ConfigureQuicGlobals();
356 net::HttpNetworkSession::Params params
;
357 InitializeNetworkSessionParams(¶ms
);
358 EXPECT_TRUE(params
.quic_enable_connection_racing
);
361 TEST_F(IOThreadTest
, QuicEnableNonBlockingIO
) {
362 field_trial_group_
= "Enabled";
363 field_trial_params_
["enable_non_blocking_io"] = "true";
364 ConfigureQuicGlobals();
365 net::HttpNetworkSession::Params params
;
366 InitializeNetworkSessionParams(¶ms
);
367 EXPECT_TRUE(params
.quic_enable_non_blocking_io
);
370 TEST_F(IOThreadTest
, QuicDisableDiskCache
) {
371 field_trial_group_
= "Enabled";
372 field_trial_params_
["disable_disk_cache"] = "true";
373 ConfigureQuicGlobals();
374 net::HttpNetworkSession::Params params
;
375 InitializeNetworkSessionParams(¶ms
);
376 EXPECT_TRUE(params
.quic_disable_disk_cache
);
379 TEST_F(IOThreadTest
, QuicPreferAes
) {
380 field_trial_group_
= "Enabled";
381 field_trial_params_
["prefer_aes"] = "true";
382 ConfigureQuicGlobals();
383 net::HttpNetworkSession::Params params
;
384 InitializeNetworkSessionParams(¶ms
);
385 EXPECT_TRUE(params
.quic_prefer_aes
);
388 TEST_F(IOThreadTest
, QuicEnableAlternativeServicesFromFieldTrialParams
) {
389 field_trial_group_
= "Enabled";
390 field_trial_params_
["use_alternative_services"] = "true";
392 ConfigureQuicGlobals();
393 net::HttpNetworkSession::Params params
;
394 InitializeNetworkSessionParams(¶ms
);
395 EXPECT_TRUE(params
.use_alternative_services
);
398 TEST_F(IOThreadTest
, QuicMaxNumberOfLossyConnectionsFieldTrialParams
) {
399 field_trial_group_
= "Enabled";
400 field_trial_params_
["max_number_of_lossy_connections"] = "5";
401 ConfigureQuicGlobals();
402 net::HttpNetworkSession::Params params
;
403 InitializeNetworkSessionParams(¶ms
);
404 EXPECT_EQ(5, params
.quic_max_number_of_lossy_connections
);
407 TEST_F(IOThreadTest
, QuicPacketLossThresholdFieldTrialParams
) {
408 field_trial_group_
= "Enabled";
409 field_trial_params_
["packet_loss_threshold"] = "0.6";
410 ConfigureQuicGlobals();
411 net::HttpNetworkSession::Params params
;
412 InitializeNetworkSessionParams(¶ms
);
413 EXPECT_EQ(0.6f
, params
.quic_packet_loss_threshold
);
416 TEST_F(IOThreadTest
, QuicReceiveBufferSize
) {
417 field_trial_group_
= "Enabled";
418 field_trial_params_
["receive_buffer_size"] = "2097152";
419 ConfigureQuicGlobals();
420 net::HttpNetworkSession::Params params
;
421 InitializeNetworkSessionParams(¶ms
);
422 EXPECT_EQ(2097152, params
.quic_socket_receive_buffer_size
);
425 TEST_F(IOThreadTest
, QuicDelayTcpConnection
) {
426 field_trial_group_
= "Enabled";
427 field_trial_params_
["delay_tcp_race"] = "true";
428 ConfigureQuicGlobals();
429 net::HttpNetworkSession::Params params
;
430 InitializeNetworkSessionParams(¶ms
);
431 EXPECT_TRUE(params
.quic_delay_tcp_race
);
434 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromFlag
) {
435 command_line_
.AppendSwitchASCII("alternative-service-probability-threshold",
438 ConfigureQuicGlobals();
439 net::HttpNetworkSession::Params params
;
440 InitializeNetworkSessionParams(¶ms
);
441 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
444 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromEnableQuicFlag
) {
445 command_line_
.AppendSwitch("enable-quic");
447 ConfigureQuicGlobals();
448 net::HttpNetworkSession::Params params
;
449 InitializeNetworkSessionParams(¶ms
);
450 EXPECT_EQ(0, params
.alternative_service_probability_threshold
);
453 // TODO(bnc): Remove when new parameter name rolls out and server configuration
455 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromOldParams
) {
456 field_trial_group_
= "Enabled";
457 field_trial_params_
["alternate_protocol_probability_threshold"] = ".5";
459 ConfigureQuicGlobals();
460 net::HttpNetworkSession::Params params
;
461 InitializeNetworkSessionParams(¶ms
);
462 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
465 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromParams
) {
466 field_trial_group_
= "Enabled";
467 field_trial_params_
["alternative_service_probability_threshold"] = ".5";
469 ConfigureQuicGlobals();
470 net::HttpNetworkSession::Params params
;
471 InitializeNetworkSessionParams(¶ms
);
472 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
475 TEST_F(IOThreadTest
, QuicDisallowedByPolicy
) {
476 command_line_
.AppendSwitch(switches::kEnableQuic
);
477 is_quic_allowed_by_policy_
= false;
478 ConfigureQuicGlobals();
480 net::HttpNetworkSession::Params params
;
481 InitializeNetworkSessionParams(¶ms
);
482 EXPECT_FALSE(params
.enable_quic
);