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 bool use_alternate_protocols
= false;
101 field_trial_group_
= "Spdy31Enabled";
102 ConfigureSpdyGlobals();
103 EXPECT_THAT(globals_
.next_protos
,
104 ElementsAre(net::kProtoHTTP11
,
106 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
107 EXPECT_TRUE(use_alternate_protocols
);
110 TEST_F(IOThreadTest
, SpdyFieldTrialSpdy4Enabled
) {
111 bool use_alternate_protocols
= false;
112 field_trial_group_
= "Spdy4Enabled";
113 ConfigureSpdyGlobals();
114 EXPECT_THAT(globals_
.next_protos
,
115 ElementsAre(net::kProtoHTTP11
, net::kProtoSPDY31
,
116 net::kProtoHTTP2_14
, net::kProtoHTTP2
));
117 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
118 EXPECT_TRUE(use_alternate_protocols
);
121 TEST_F(IOThreadTest
, SpdyFieldTrialDefault
) {
122 field_trial_group_
= "";
123 ConfigureSpdyGlobals();
124 EXPECT_THAT(globals_
.next_protos
,
125 ElementsAre(net::kProtoHTTP11
, net::kProtoSPDY31
,
126 net::kProtoHTTP2_14
, net::kProtoHTTP2
));
127 bool use_alternate_protocols
= false;
128 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
129 EXPECT_TRUE(use_alternate_protocols
);
132 TEST_F(IOThreadTest
, SpdyFieldTrialParametrized
) {
133 field_trial_params_
["enable_spdy31"] = "false";
134 // Undefined parameter "enable_http2_14" should default to false.
135 field_trial_params_
["enable_http2"] = "true";
136 field_trial_group_
= "ParametrizedHTTP2Only";
137 ConfigureSpdyGlobals();
138 EXPECT_THAT(globals_
.next_protos
,
139 ElementsAre(net::kProtoHTTP11
, net::kProtoHTTP2
));
140 bool use_alternate_protocols
= false;
141 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
142 EXPECT_TRUE(use_alternate_protocols
);
145 TEST_F(IOThreadTest
, SpdyCommandLineUseSpdyOff
) {
146 command_line_
.AppendSwitchASCII("use-spdy", "off");
147 // Command line should overwrite field trial group.
148 field_trial_group_
= "Spdy4Enabled";
149 ConfigureSpdyGlobals();
150 EXPECT_EQ(0u, globals_
.next_protos
.size());
153 TEST_F(IOThreadTest
, SpdyCommandLineUseSpdyDisableAltProtocols
) {
154 command_line_
.AppendSwitchASCII("use-spdy", "no-alt-protocols");
155 ConfigureSpdyGlobals();
156 bool use_alternate_protocols
= true;
157 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
158 EXPECT_FALSE(use_alternate_protocols
);
161 TEST_F(IOThreadTest
, DisableQuicByDefault
) {
162 ConfigureQuicGlobals();
163 net::HttpNetworkSession::Params params
;
164 InitializeNetworkSessionParams(¶ms
);
165 EXPECT_FALSE(params
.enable_quic
);
166 EXPECT_FALSE(params
.enable_quic_for_proxies
);
167 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
170 TEST_F(IOThreadTest
, EnableQuicFromFieldTrialGroup
) {
171 field_trial_group_
= "Enabled";
173 ConfigureQuicGlobals();
174 net::HttpNetworkSession::Params default_params
;
175 net::HttpNetworkSession::Params params
;
176 InitializeNetworkSessionParams(¶ms
);
177 EXPECT_TRUE(params
.enable_quic
);
178 EXPECT_FALSE(params
.disable_insecure_quic
);
179 EXPECT_TRUE(params
.enable_quic_for_proxies
);
180 EXPECT_EQ(1350u, params
.quic_max_packet_length
);
181 EXPECT_EQ(1.0, params
.alternative_service_probability_threshold
);
182 EXPECT_EQ(default_params
.quic_supported_versions
,
183 params
.quic_supported_versions
);
184 EXPECT_EQ(net::QuicTagVector(), params
.quic_connection_options
);
185 EXPECT_FALSE(params
.quic_always_require_handshake_confirmation
);
186 EXPECT_FALSE(params
.quic_disable_connection_pooling
);
187 EXPECT_EQ(0.25f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
188 EXPECT_FALSE(params
.quic_enable_connection_racing
);
189 EXPECT_FALSE(params
.quic_enable_non_blocking_io
);
190 EXPECT_FALSE(params
.quic_disable_disk_cache
);
191 EXPECT_FALSE(params
.quic_prefer_aes
);
192 EXPECT_EQ(0, params
.quic_max_number_of_lossy_connections
);
193 EXPECT_EQ(1.0f
, params
.quic_packet_loss_threshold
);
194 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
197 TEST_F(IOThreadTest
, EnableQuicFromQuicProxyFieldTrialGroup
) {
198 base::FieldTrialList
field_trial_list(new base::MockEntropyProvider());
199 base::FieldTrialList::CreateFieldTrial(
200 data_reduction_proxy::params::GetQuicFieldTrialName(), "Enabled");
202 ConfigureQuicGlobals();
203 net::HttpNetworkSession::Params params
;
204 InitializeNetworkSessionParams(¶ms
);
205 EXPECT_FALSE(params
.enable_quic
);
206 EXPECT_TRUE(params
.enable_quic_for_proxies
);
207 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
208 EXPECT_EQ(1024 * 1024, params
.quic_socket_receive_buffer_size
);
211 TEST_F(IOThreadTest
, EnableQuicFromCommandLine
) {
212 command_line_
.AppendSwitch("enable-quic");
214 ConfigureQuicGlobals();
215 net::HttpNetworkSession::Params params
;
216 InitializeNetworkSessionParams(¶ms
);
217 EXPECT_TRUE(params
.enable_quic
);
218 EXPECT_TRUE(params
.enable_quic_for_proxies
);
219 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
222 TEST_F(IOThreadTest
, DisableInsecureQuicFromFieldTrialParams
) {
223 field_trial_group_
= "Enabled";
224 field_trial_params_
["disable_insecure_quic"] = "true";
226 ConfigureQuicGlobals();
227 net::HttpNetworkSession::Params params
;
228 InitializeNetworkSessionParams(¶ms
);
229 EXPECT_TRUE(params
.disable_insecure_quic
);
232 TEST_F(IOThreadTest
, PacketLengthFromCommandLine
) {
233 command_line_
.AppendSwitch("enable-quic");
234 command_line_
.AppendSwitchASCII("quic-max-packet-length", "1450");
236 ConfigureQuicGlobals();
237 net::HttpNetworkSession::Params params
;
238 InitializeNetworkSessionParams(¶ms
);
239 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
242 TEST_F(IOThreadTest
, PacketLengthFromFieldTrialParams
) {
243 field_trial_group_
= "Enabled";
244 field_trial_params_
["max_packet_length"] = "1450";
246 ConfigureQuicGlobals();
247 net::HttpNetworkSession::Params params
;
248 InitializeNetworkSessionParams(¶ms
);
249 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
252 TEST_F(IOThreadTest
, QuicVersionFromCommandLine
) {
253 command_line_
.AppendSwitch("enable-quic");
254 std::string version
=
255 net::QuicVersionToString(net::QuicSupportedVersions().back());
256 command_line_
.AppendSwitchASCII("quic-version", version
);
258 ConfigureQuicGlobals();
259 net::HttpNetworkSession::Params params
;
260 InitializeNetworkSessionParams(¶ms
);
261 net::QuicVersionVector supported_versions
;
262 supported_versions
.push_back(net::QuicSupportedVersions().back());
263 EXPECT_EQ(supported_versions
, params
.quic_supported_versions
);
266 TEST_F(IOThreadTest
, QuicVersionFromFieldTrialParams
) {
267 field_trial_group_
= "Enabled";
268 field_trial_params_
["quic_version"] =
269 net::QuicVersionToString(net::QuicSupportedVersions().back());
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
, QuicConnectionOptionsFromCommandLine
) {
280 command_line_
.AppendSwitch("enable-quic");
281 command_line_
.AppendSwitchASCII("quic-connection-options",
284 ConfigureQuicGlobals();
285 net::HttpNetworkSession::Params params
;
286 InitializeNetworkSessionParams(¶ms
);
288 net::QuicTagVector options
;
289 options
.push_back(net::kTIME
);
290 options
.push_back(net::kTBBR
);
291 options
.push_back(net::kREJ
);
292 EXPECT_EQ(options
, params
.quic_connection_options
);
295 TEST_F(IOThreadTest
, QuicConnectionOptionsFromFieldTrialParams
) {
296 field_trial_group_
= "Enabled";
297 field_trial_params_
["connection_options"] = "TIME,TBBR,REJ";
299 ConfigureQuicGlobals();
300 net::HttpNetworkSession::Params params
;
301 InitializeNetworkSessionParams(¶ms
);
303 net::QuicTagVector options
;
304 options
.push_back(net::kTIME
);
305 options
.push_back(net::kTBBR
);
306 options
.push_back(net::kREJ
);
307 EXPECT_EQ(options
, params
.quic_connection_options
);
311 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams
) {
312 field_trial_group_
= "Enabled";
313 field_trial_params_
["always_require_handshake_confirmation"] = "true";
314 ConfigureQuicGlobals();
315 net::HttpNetworkSession::Params params
;
316 InitializeNetworkSessionParams(¶ms
);
317 EXPECT_TRUE(params
.quic_always_require_handshake_confirmation
);
321 QuicDisableConnectionPoolingFromFieldTrialParams
) {
322 field_trial_group_
= "Enabled";
323 field_trial_params_
["disable_connection_pooling"] = "true";
324 ConfigureQuicGlobals();
325 net::HttpNetworkSession::Params params
;
326 InitializeNetworkSessionParams(¶ms
);
327 EXPECT_TRUE(params
.quic_disable_connection_pooling
);
330 TEST_F(IOThreadTest
, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams
) {
331 field_trial_group_
= "Enabled";
332 field_trial_params_
["load_server_info_time_to_srtt"] = "0.5";
333 ConfigureQuicGlobals();
334 net::HttpNetworkSession::Params params
;
335 InitializeNetworkSessionParams(¶ms
);
336 EXPECT_EQ(0.5f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
339 TEST_F(IOThreadTest
, QuicEnableConnectionRacing
) {
340 field_trial_group_
= "Enabled";
341 field_trial_params_
["enable_connection_racing"] = "true";
342 ConfigureQuicGlobals();
343 net::HttpNetworkSession::Params params
;
344 InitializeNetworkSessionParams(¶ms
);
345 EXPECT_TRUE(params
.quic_enable_connection_racing
);
348 TEST_F(IOThreadTest
, QuicEnableNonBlockingIO
) {
349 field_trial_group_
= "Enabled";
350 field_trial_params_
["enable_non_blocking_io"] = "true";
351 ConfigureQuicGlobals();
352 net::HttpNetworkSession::Params params
;
353 InitializeNetworkSessionParams(¶ms
);
354 EXPECT_TRUE(params
.quic_enable_non_blocking_io
);
357 TEST_F(IOThreadTest
, QuicDisableDiskCache
) {
358 field_trial_group_
= "Enabled";
359 field_trial_params_
["disable_disk_cache"] = "true";
360 ConfigureQuicGlobals();
361 net::HttpNetworkSession::Params params
;
362 InitializeNetworkSessionParams(¶ms
);
363 EXPECT_TRUE(params
.quic_disable_disk_cache
);
366 TEST_F(IOThreadTest
, QuicPreferAes
) {
367 field_trial_group_
= "Enabled";
368 field_trial_params_
["prefer_aes"] = "true";
369 ConfigureQuicGlobals();
370 net::HttpNetworkSession::Params params
;
371 InitializeNetworkSessionParams(¶ms
);
372 EXPECT_TRUE(params
.quic_prefer_aes
);
375 TEST_F(IOThreadTest
, QuicMaxNumberOfLossyConnectionsFieldTrialParams
) {
376 field_trial_group_
= "Enabled";
377 field_trial_params_
["max_number_of_lossy_connections"] = "5";
378 ConfigureQuicGlobals();
379 net::HttpNetworkSession::Params params
;
380 InitializeNetworkSessionParams(¶ms
);
381 EXPECT_EQ(5, params
.quic_max_number_of_lossy_connections
);
384 TEST_F(IOThreadTest
, QuicPacketLossThresholdFieldTrialParams
) {
385 field_trial_group_
= "Enabled";
386 field_trial_params_
["packet_loss_threshold"] = "0.5";
387 ConfigureQuicGlobals();
388 net::HttpNetworkSession::Params params
;
389 InitializeNetworkSessionParams(¶ms
);
390 EXPECT_EQ(0.5f
, params
.quic_packet_loss_threshold
);
393 TEST_F(IOThreadTest
, QuicReceiveBufferSize
) {
394 field_trial_group_
= "Enabled";
395 field_trial_params_
["receive_buffer_size"] = "2097152";
396 ConfigureQuicGlobals();
397 net::HttpNetworkSession::Params params
;
398 InitializeNetworkSessionParams(¶ms
);
399 EXPECT_EQ(2097152, params
.quic_socket_receive_buffer_size
);
402 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromFlag
) {
403 command_line_
.AppendSwitchASCII("alternative-service-probability-threshold",
406 ConfigureQuicGlobals();
407 net::HttpNetworkSession::Params params
;
408 InitializeNetworkSessionParams(¶ms
);
409 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
412 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromEnableQuicFlag
) {
413 command_line_
.AppendSwitch("enable-quic");
415 ConfigureQuicGlobals();
416 net::HttpNetworkSession::Params params
;
417 InitializeNetworkSessionParams(¶ms
);
418 EXPECT_EQ(0, params
.alternative_service_probability_threshold
);
421 // TODO(bnc): Remove when new parameter name rolls out and server configuration
423 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromOldParams
) {
424 field_trial_group_
= "Enabled";
425 field_trial_params_
["alternate_protocol_probability_threshold"] = ".5";
427 ConfigureQuicGlobals();
428 net::HttpNetworkSession::Params params
;
429 InitializeNetworkSessionParams(¶ms
);
430 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
433 TEST_F(IOThreadTest
, AlternativeServiceProbabilityThresholdFromParams
) {
434 field_trial_group_
= "Enabled";
435 field_trial_params_
["alternative_service_probability_threshold"] = ".5";
437 ConfigureQuicGlobals();
438 net::HttpNetworkSession::Params params
;
439 InitializeNetworkSessionParams(¶ms
);
440 EXPECT_EQ(.5, params
.alternative_service_probability_threshold
);
443 TEST_F(IOThreadTest
, QuicDisallowedByPolicy
) {
444 command_line_
.AppendSwitch(switches::kEnableQuic
);
445 is_quic_allowed_by_policy_
= false;
446 ConfigureQuicGlobals();
448 net::HttpNetworkSession::Params params
;
449 InitializeNetworkSessionParams(¶ms
);
450 EXPECT_FALSE(params
.enable_quic
);