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 "chrome/browser/io_thread.h"
8 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
9 #include "net/http/http_network_session.h"
10 #include "net/http/http_server_properties_impl.h"
11 #include "net/quic/quic_protocol.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::ElementsAre
;
19 class BadEntropyProvider
: public base::FieldTrial::EntropyProvider
{
21 ~BadEntropyProvider() override
{}
23 double GetEntropyForTrial(const std::string
& trial_name
,
24 uint32 randomization_seed
) const override
{
31 static void ConfigureQuicGlobals(
32 const base::CommandLine
& command_line
,
33 base::StringPiece quic_trial_group
,
34 const std::map
<std::string
, std::string
>& quic_trial_params
,
35 IOThread::Globals
* globals
) {
36 IOThread::ConfigureQuicGlobals(command_line
, quic_trial_group
,
37 quic_trial_params
, globals
);
40 static void InitializeNetworkSessionParamsFromGlobals(
41 const IOThread::Globals
& globals
,
42 net::HttpNetworkSession::Params
* params
) {
43 IOThread::InitializeNetworkSessionParamsFromGlobals(globals
, params
);
46 static void ConfigureSpdyFromTrial(const std::string
& trial_group
,
47 IOThread::Globals
* globals
) {
48 IOThread::ConfigureSpdyFromTrial(trial_group
, globals
);
52 class IOThreadTest
: public testing::Test
{
54 IOThreadTest() : command_line_(base::CommandLine::NO_PROGRAM
) {
55 globals_
.http_server_properties
.reset(new net::HttpServerPropertiesImpl());
58 void ConfigureQuicGlobals() {
59 IOThreadPeer::ConfigureQuicGlobals(command_line_
, field_trial_group_
,
60 field_trial_params_
, &globals_
);
63 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params
* params
) {
64 IOThreadPeer::InitializeNetworkSessionParamsFromGlobals(globals_
, params
);
67 base::CommandLine command_line_
;
68 IOThread::Globals globals_
;
69 std::string field_trial_group_
;
70 std::map
<std::string
, std::string
> field_trial_params_
;
73 TEST_F(IOThreadTest
, InitializeNetworkSessionParamsFromGlobals
) {
74 globals_
.quic_connection_options
.push_back(net::kPACE
);
75 globals_
.quic_connection_options
.push_back(net::kTBBR
);
76 globals_
.quic_connection_options
.push_back(net::kTIME
);
78 net::HttpNetworkSession::Params params
;
79 InitializeNetworkSessionParams(¶ms
);
80 EXPECT_EQ(globals_
.quic_connection_options
,
81 params
.quic_connection_options
);
84 TEST_F(IOThreadTest
, SpdyFieldTrialHoldbackEnabled
) {
85 net::HttpStreamFactory::set_spdy_enabled(true);
86 IOThreadPeer::ConfigureSpdyFromTrial("SpdyDisabled", &globals_
);
87 EXPECT_FALSE(net::HttpStreamFactory::spdy_enabled());
90 TEST_F(IOThreadTest
, SpdyFieldTrialSpdy31Enabled
) {
91 bool use_alternate_protocols
= false;
92 IOThreadPeer::ConfigureSpdyFromTrial("Spdy31Enabled", &globals_
);
93 EXPECT_THAT(globals_
.next_protos
,
94 ElementsAre(net::kProtoHTTP11
,
95 net::kProtoQUIC1SPDY3
,
97 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
98 EXPECT_TRUE(use_alternate_protocols
);
101 TEST_F(IOThreadTest
, SpdyFieldTrialSpdy4Enabled
) {
102 bool use_alternate_protocols
= false;
103 IOThreadPeer::ConfigureSpdyFromTrial("Spdy4Enabled", &globals_
);
104 EXPECT_THAT(globals_
.next_protos
,
105 ElementsAre(net::kProtoHTTP11
,
106 net::kProtoQUIC1SPDY3
,
108 net::kProtoSPDY4_14
));
109 globals_
.use_alternate_protocols
.CopyToIfSet(&use_alternate_protocols
);
110 EXPECT_TRUE(use_alternate_protocols
);
113 TEST_F(IOThreadTest
, DisableQuicByDefault
) {
114 ConfigureQuicGlobals();
115 net::HttpNetworkSession::Params params
;
116 InitializeNetworkSessionParams(¶ms
);
117 EXPECT_FALSE(params
.enable_quic
);
118 EXPECT_FALSE(params
.enable_quic_for_proxies
);
119 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
122 TEST_F(IOThreadTest
, EnableQuicFromFieldTrialGroup
) {
123 field_trial_group_
= "Enabled";
125 ConfigureQuicGlobals();
126 net::HttpNetworkSession::Params default_params
;
127 net::HttpNetworkSession::Params params
;
128 InitializeNetworkSessionParams(¶ms
);
129 EXPECT_TRUE(params
.enable_quic
);
130 EXPECT_TRUE(params
.enable_quic_for_proxies
);
131 EXPECT_EQ(1350u, params
.quic_max_packet_length
);
132 EXPECT_EQ(1.0, params
.alternate_protocol_probability_threshold
);
133 EXPECT_EQ(default_params
.quic_supported_versions
,
134 params
.quic_supported_versions
);
135 EXPECT_EQ(net::QuicTagVector(), params
.quic_connection_options
);
136 EXPECT_FALSE(params
.quic_always_require_handshake_confirmation
);
137 EXPECT_FALSE(params
.quic_disable_connection_pooling
);
138 EXPECT_EQ(0, params
.quic_load_server_info_timeout_ms
);
139 EXPECT_EQ(0.0f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
140 EXPECT_FALSE(params
.quic_enable_truncated_connection_ids
);
141 EXPECT_FALSE(params
.quic_enable_connection_racing
);
142 EXPECT_FALSE(params
.quic_enable_non_blocking_io
);
143 EXPECT_FALSE(params
.quic_disable_disk_cache
);
144 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
147 TEST_F(IOThreadTest
, EnableQuicFromQuicProxyFieldTrialGroup
) {
148 base::FieldTrialList
field_trial_list(new BadEntropyProvider());
149 base::FieldTrialList::CreateFieldTrial(
150 data_reduction_proxy::DataReductionProxyParams::GetQuicFieldTrialName(),
153 ConfigureQuicGlobals();
154 net::HttpNetworkSession::Params params
;
155 InitializeNetworkSessionParams(¶ms
);
156 EXPECT_FALSE(params
.enable_quic
);
157 EXPECT_TRUE(params
.enable_quic_for_proxies
);
158 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
159 EXPECT_EQ(256 * 1024, params
.quic_socket_receive_buffer_size
);
162 TEST_F(IOThreadTest
, EnableQuicFromCommandLine
) {
163 command_line_
.AppendSwitch("enable-quic");
165 ConfigureQuicGlobals();
166 net::HttpNetworkSession::Params params
;
167 InitializeNetworkSessionParams(¶ms
);
168 EXPECT_TRUE(params
.enable_quic
);
169 EXPECT_TRUE(params
.enable_quic_for_proxies
);
170 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
173 TEST_F(IOThreadTest
, EnablePacingFromCommandLine
) {
174 command_line_
.AppendSwitch("enable-quic");
175 command_line_
.AppendSwitch("enable-quic-pacing");
177 ConfigureQuicGlobals();
178 net::HttpNetworkSession::Params params
;
179 InitializeNetworkSessionParams(¶ms
);
180 net::QuicTagVector options
;
181 options
.push_back(net::kPACE
);
182 EXPECT_EQ(options
, params
.quic_connection_options
);
185 TEST_F(IOThreadTest
, EnablePacingFromFieldTrialParams
) {
186 field_trial_group_
= "Enabled";
187 field_trial_params_
["enable_pacing"] = "true";
189 ConfigureQuicGlobals();
190 net::HttpNetworkSession::Params params
;
191 InitializeNetworkSessionParams(¶ms
);
192 net::QuicTagVector options
;
193 options
.push_back(net::kPACE
);
194 EXPECT_EQ(options
, params
.quic_connection_options
);
197 TEST_F(IOThreadTest
, PacketLengthFromCommandLine
) {
198 command_line_
.AppendSwitch("enable-quic");
199 command_line_
.AppendSwitchASCII("quic-max-packet-length", "1450");
201 ConfigureQuicGlobals();
202 net::HttpNetworkSession::Params params
;
203 InitializeNetworkSessionParams(¶ms
);
204 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
207 TEST_F(IOThreadTest
, PacketLengthFromFieldTrialParams
) {
208 field_trial_group_
= "Enabled";
209 field_trial_params_
["max_packet_length"] = "1450";
211 ConfigureQuicGlobals();
212 net::HttpNetworkSession::Params params
;
213 InitializeNetworkSessionParams(¶ms
);
214 EXPECT_EQ(1450u, params
.quic_max_packet_length
);
217 TEST_F(IOThreadTest
, QuicVersionFromCommandLine
) {
218 command_line_
.AppendSwitch("enable-quic");
219 std::string version
=
220 net::QuicVersionToString(net::QuicSupportedVersions().back());
221 command_line_
.AppendSwitchASCII("quic-version", version
);
223 ConfigureQuicGlobals();
224 net::HttpNetworkSession::Params params
;
225 InitializeNetworkSessionParams(¶ms
);
226 net::QuicVersionVector supported_versions
;
227 supported_versions
.push_back(net::QuicSupportedVersions().back());
228 EXPECT_EQ(supported_versions
, params
.quic_supported_versions
);
231 TEST_F(IOThreadTest
, QuicVersionFromFieldTrialParams
) {
232 field_trial_group_
= "Enabled";
233 field_trial_params_
["quic_version"] =
234 net::QuicVersionToString(net::QuicSupportedVersions().back());
236 ConfigureQuicGlobals();
237 net::HttpNetworkSession::Params params
;
238 InitializeNetworkSessionParams(¶ms
);
239 net::QuicVersionVector supported_versions
;
240 supported_versions
.push_back(net::QuicSupportedVersions().back());
241 EXPECT_EQ(supported_versions
, params
.quic_supported_versions
);
244 TEST_F(IOThreadTest
, QuicConnectionOptionsFromCommandLine
) {
245 command_line_
.AppendSwitch("enable-quic");
246 command_line_
.AppendSwitchASCII("quic-connection-options",
247 "PACE,TIME,TBBR,REJ");
249 ConfigureQuicGlobals();
250 net::HttpNetworkSession::Params params
;
251 InitializeNetworkSessionParams(¶ms
);
253 net::QuicTagVector options
;
254 options
.push_back(net::kPACE
);
255 options
.push_back(net::kTIME
);
256 options
.push_back(net::kTBBR
);
257 options
.push_back(net::kREJ
);
258 EXPECT_EQ(options
, params
.quic_connection_options
);
261 TEST_F(IOThreadTest
, QuicConnectionOptionsFromFieldTrialParams
) {
262 field_trial_group_
= "Enabled";
263 field_trial_params_
["connection_options"] = "PACE,TIME,TBBR,REJ";
265 ConfigureQuicGlobals();
266 net::HttpNetworkSession::Params params
;
267 InitializeNetworkSessionParams(¶ms
);
269 net::QuicTagVector options
;
270 options
.push_back(net::kPACE
);
271 options
.push_back(net::kTIME
);
272 options
.push_back(net::kTBBR
);
273 options
.push_back(net::kREJ
);
274 EXPECT_EQ(options
, params
.quic_connection_options
);
278 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams
) {
279 field_trial_group_
= "Enabled";
280 field_trial_params_
["always_require_handshake_confirmation"] = "true";
281 ConfigureQuicGlobals();
282 net::HttpNetworkSession::Params params
;
283 InitializeNetworkSessionParams(¶ms
);
284 EXPECT_TRUE(params
.quic_always_require_handshake_confirmation
);
288 QuicDisableConnectionPoolingFromFieldTrialParams
) {
289 field_trial_group_
= "Enabled";
290 field_trial_params_
["disable_connection_pooling"] = "true";
291 ConfigureQuicGlobals();
292 net::HttpNetworkSession::Params params
;
293 InitializeNetworkSessionParams(¶ms
);
294 EXPECT_TRUE(params
.quic_disable_connection_pooling
);
297 TEST_F(IOThreadTest
, QuicLoadServerInfoTimeoutFromFieldTrialParams
) {
298 field_trial_group_
= "Enabled";
299 field_trial_params_
["load_server_info_timeout"] = "50";
300 ConfigureQuicGlobals();
301 net::HttpNetworkSession::Params params
;
302 InitializeNetworkSessionParams(¶ms
);
303 EXPECT_EQ(50, params
.quic_load_server_info_timeout_ms
);
306 TEST_F(IOThreadTest
, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams
) {
307 field_trial_group_
= "Enabled";
308 field_trial_params_
["load_server_info_time_to_srtt"] = "0.5";
309 ConfigureQuicGlobals();
310 net::HttpNetworkSession::Params params
;
311 InitializeNetworkSessionParams(¶ms
);
312 EXPECT_EQ(0.5f
, params
.quic_load_server_info_timeout_srtt_multiplier
);
315 TEST_F(IOThreadTest
, QuicEnableTruncatedConnectionIds
) {
316 field_trial_group_
= "Enabled";
317 field_trial_params_
["enable_truncated_connection_ids"] = "true";
318 ConfigureQuicGlobals();
319 net::HttpNetworkSession::Params params
;
320 InitializeNetworkSessionParams(¶ms
);
321 EXPECT_TRUE(params
.quic_enable_truncated_connection_ids
);
324 TEST_F(IOThreadTest
, QuicEnableConnectionRacing
) {
325 field_trial_group_
= "Enabled";
326 field_trial_params_
["enable_connection_racing"] = "true";
327 ConfigureQuicGlobals();
328 net::HttpNetworkSession::Params params
;
329 InitializeNetworkSessionParams(¶ms
);
330 EXPECT_TRUE(params
.quic_enable_connection_racing
);
333 TEST_F(IOThreadTest
, QuicEnableNonBlockingIO
) {
334 field_trial_group_
= "Enabled";
335 field_trial_params_
["enable_non_blocking_io"] = "true";
336 ConfigureQuicGlobals();
337 net::HttpNetworkSession::Params params
;
338 InitializeNetworkSessionParams(¶ms
);
339 EXPECT_TRUE(params
.quic_enable_non_blocking_io
);
342 TEST_F(IOThreadTest
, QuicDisableDiskCache
) {
343 field_trial_group_
= "Enabled";
344 field_trial_params_
["disable_disk_cache"] = "true";
345 ConfigureQuicGlobals();
346 net::HttpNetworkSession::Params params
;
347 InitializeNetworkSessionParams(¶ms
);
348 EXPECT_TRUE(params
.quic_disable_disk_cache
);
351 TEST_F(IOThreadTest
, QuicReceiveBufferSize
) {
352 field_trial_group_
= "Enabled";
353 field_trial_params_
["receive_buffer_size"] = "1048576";
354 ConfigureQuicGlobals();
355 net::HttpNetworkSession::Params params
;
356 InitializeNetworkSessionParams(¶ms
);
357 EXPECT_EQ(1048576, params
.quic_socket_receive_buffer_size
);
361 AlternateProtocolProbabilityThresholdFromFlag
) {
362 command_line_
.AppendSwitchASCII("alternate-protocol-probability-threshold",
365 ConfigureQuicGlobals();
366 net::HttpNetworkSession::Params params
;
367 InitializeNetworkSessionParams(¶ms
);
368 EXPECT_EQ(.5, params
.alternate_protocol_probability_threshold
);
372 AlternateProtocolProbabilityThresholdFromEnableQuicFlag
) {
373 command_line_
.AppendSwitch("enable-quic");
375 ConfigureQuicGlobals();
376 net::HttpNetworkSession::Params params
;
377 InitializeNetworkSessionParams(¶ms
);
378 EXPECT_EQ(0, params
.alternate_protocol_probability_threshold
);
382 AlternateProtocolProbabilityThresholdFromParams
) {
383 field_trial_group_
= "Enabled";
384 field_trial_params_
["alternate_protocol_probability_threshold"] = ".5";
386 ConfigureQuicGlobals();
387 net::HttpNetworkSession::Params params
;
388 InitializeNetworkSessionParams(¶ms
);
389 EXPECT_EQ(.5, params
.alternate_protocol_probability_threshold
);