Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / io_thread_unittest.cc
blob2d6a6c607fbe08a9ef70ffbe845c512a097b75bc
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"
17 namespace test {
19 using ::testing::ElementsAre;
21 class IOThreadPeer {
22 public:
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,
31 globals);
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 {
51 public:
52 IOThreadTest()
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_,
60 field_trial_group_,
61 field_trial_params_,
62 is_quic_allowed_by_policy_,
63 &globals_);
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(&params);
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,
104 net::kProtoSPDY31));
107 TEST_F(IOThreadTest, SpdyFieldTrialSpdy4Enabled) {
108 field_trial_group_ = "Spdy4Enabled";
109 ConfigureSpdyGlobals();
110 EXPECT_THAT(
111 globals_.next_protos,
112 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31, net::kProtoHTTP2));
115 TEST_F(IOThreadTest, SpdyFieldTrialDefault) {
116 field_trial_group_ = "";
117 ConfigureSpdyGlobals();
118 EXPECT_THAT(
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(&params);
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(&params);
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(IOThread::ShouldEnableQuicForDataReductionProxy());
178 TEST_F(IOThreadTest, EnableQuicFromQuicProxyFieldTrialGroup) {
179 base::FieldTrialList field_trial_list(new base::MockEntropyProvider());
180 base::FieldTrialList::CreateFieldTrial(
181 data_reduction_proxy::params::GetQuicFieldTrialName(), "Enabled");
183 ConfigureQuicGlobals();
184 net::HttpNetworkSession::Params params;
185 InitializeNetworkSessionParams(&params);
186 EXPECT_FALSE(params.enable_quic);
187 EXPECT_TRUE(params.enable_quic_for_proxies);
188 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
189 EXPECT_EQ(1024 * 1024, params.quic_socket_receive_buffer_size);
192 TEST_F(IOThreadTest, EnableQuicFromCommandLine) {
193 command_line_.AppendSwitch("enable-quic");
195 ConfigureQuicGlobals();
196 net::HttpNetworkSession::Params params;
197 InitializeNetworkSessionParams(&params);
198 EXPECT_TRUE(params.enable_quic);
199 EXPECT_TRUE(params.enable_quic_for_proxies);
200 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
203 TEST_F(IOThreadTest, EnableInsecureQuicFromFieldTrialParams) {
204 field_trial_group_ = "Enabled";
205 field_trial_params_["enable_insecure_quic"] = "true";
207 ConfigureQuicGlobals();
208 net::HttpNetworkSession::Params params;
209 InitializeNetworkSessionParams(&params);
210 EXPECT_TRUE(params.enable_insecure_quic);
213 TEST_F(IOThreadTest, EnableInsecureQuicCommandLine) {
214 command_line_.AppendSwitch("enable-quic");
215 command_line_.AppendSwitch("enable-insecure-quic");
217 ConfigureQuicGlobals();
218 net::HttpNetworkSession::Params params;
219 InitializeNetworkSessionParams(&params);
220 EXPECT_TRUE(params.enable_insecure_quic);
223 TEST_F(IOThreadTest, PacketLengthFromCommandLine) {
224 command_line_.AppendSwitch("enable-quic");
225 command_line_.AppendSwitchASCII("quic-max-packet-length", "1450");
227 ConfigureQuicGlobals();
228 net::HttpNetworkSession::Params params;
229 InitializeNetworkSessionParams(&params);
230 EXPECT_EQ(1450u, params.quic_max_packet_length);
233 TEST_F(IOThreadTest, PacketLengthFromFieldTrialParams) {
234 field_trial_group_ = "Enabled";
235 field_trial_params_["max_packet_length"] = "1450";
237 ConfigureQuicGlobals();
238 net::HttpNetworkSession::Params params;
239 InitializeNetworkSessionParams(&params);
240 EXPECT_EQ(1450u, params.quic_max_packet_length);
243 TEST_F(IOThreadTest, QuicVersionFromCommandLine) {
244 command_line_.AppendSwitch("enable-quic");
245 std::string version =
246 net::QuicVersionToString(net::QuicSupportedVersions().back());
247 command_line_.AppendSwitchASCII("quic-version", version);
249 ConfigureQuicGlobals();
250 net::HttpNetworkSession::Params params;
251 InitializeNetworkSessionParams(&params);
252 net::QuicVersionVector supported_versions;
253 supported_versions.push_back(net::QuicSupportedVersions().back());
254 EXPECT_EQ(supported_versions, params.quic_supported_versions);
257 TEST_F(IOThreadTest, QuicVersionFromFieldTrialParams) {
258 field_trial_group_ = "Enabled";
259 field_trial_params_["quic_version"] =
260 net::QuicVersionToString(net::QuicSupportedVersions().back());
262 ConfigureQuicGlobals();
263 net::HttpNetworkSession::Params params;
264 InitializeNetworkSessionParams(&params);
265 net::QuicVersionVector supported_versions;
266 supported_versions.push_back(net::QuicSupportedVersions().back());
267 EXPECT_EQ(supported_versions, params.quic_supported_versions);
270 TEST_F(IOThreadTest, QuicConnectionOptionsFromCommandLine) {
271 command_line_.AppendSwitch("enable-quic");
272 command_line_.AppendSwitchASCII("quic-connection-options",
273 "TIME,TBBR,REJ");
275 ConfigureQuicGlobals();
276 net::HttpNetworkSession::Params params;
277 InitializeNetworkSessionParams(&params);
279 net::QuicTagVector options;
280 options.push_back(net::kTIME);
281 options.push_back(net::kTBBR);
282 options.push_back(net::kREJ);
283 EXPECT_EQ(options, params.quic_connection_options);
286 TEST_F(IOThreadTest, QuicConnectionOptionsFromFieldTrialParams) {
287 field_trial_group_ = "Enabled";
288 field_trial_params_["connection_options"] = "TIME,TBBR,REJ";
290 ConfigureQuicGlobals();
291 net::HttpNetworkSession::Params params;
292 InitializeNetworkSessionParams(&params);
294 net::QuicTagVector options;
295 options.push_back(net::kTIME);
296 options.push_back(net::kTBBR);
297 options.push_back(net::kREJ);
298 EXPECT_EQ(options, params.quic_connection_options);
301 TEST_F(IOThreadTest,
302 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams) {
303 field_trial_group_ = "Enabled";
304 field_trial_params_["always_require_handshake_confirmation"] = "true";
305 ConfigureQuicGlobals();
306 net::HttpNetworkSession::Params params;
307 InitializeNetworkSessionParams(&params);
308 EXPECT_TRUE(params.quic_always_require_handshake_confirmation);
311 TEST_F(IOThreadTest,
312 QuicDisableConnectionPoolingFromFieldTrialParams) {
313 field_trial_group_ = "Enabled";
314 field_trial_params_["disable_connection_pooling"] = "true";
315 ConfigureQuicGlobals();
316 net::HttpNetworkSession::Params params;
317 InitializeNetworkSessionParams(&params);
318 EXPECT_TRUE(params.quic_disable_connection_pooling);
321 TEST_F(IOThreadTest, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams) {
322 field_trial_group_ = "Enabled";
323 field_trial_params_["load_server_info_time_to_srtt"] = "0.5";
324 ConfigureQuicGlobals();
325 net::HttpNetworkSession::Params params;
326 InitializeNetworkSessionParams(&params);
327 EXPECT_EQ(0.5f, params.quic_load_server_info_timeout_srtt_multiplier);
330 TEST_F(IOThreadTest, QuicEnableConnectionRacing) {
331 field_trial_group_ = "Enabled";
332 field_trial_params_["enable_connection_racing"] = "true";
333 ConfigureQuicGlobals();
334 net::HttpNetworkSession::Params params;
335 InitializeNetworkSessionParams(&params);
336 EXPECT_TRUE(params.quic_enable_connection_racing);
339 TEST_F(IOThreadTest, QuicEnableNonBlockingIO) {
340 field_trial_group_ = "Enabled";
341 field_trial_params_["enable_non_blocking_io"] = "true";
342 ConfigureQuicGlobals();
343 net::HttpNetworkSession::Params params;
344 InitializeNetworkSessionParams(&params);
345 EXPECT_TRUE(params.quic_enable_non_blocking_io);
348 TEST_F(IOThreadTest, QuicDisableDiskCache) {
349 field_trial_group_ = "Enabled";
350 field_trial_params_["disable_disk_cache"] = "true";
351 ConfigureQuicGlobals();
352 net::HttpNetworkSession::Params params;
353 InitializeNetworkSessionParams(&params);
354 EXPECT_TRUE(params.quic_disable_disk_cache);
357 TEST_F(IOThreadTest, QuicPreferAes) {
358 field_trial_group_ = "Enabled";
359 field_trial_params_["prefer_aes"] = "true";
360 ConfigureQuicGlobals();
361 net::HttpNetworkSession::Params params;
362 InitializeNetworkSessionParams(&params);
363 EXPECT_TRUE(params.quic_prefer_aes);
366 TEST_F(IOThreadTest, QuicEnableAlternativeServicesFromFieldTrialParams) {
367 field_trial_group_ = "Enabled";
368 field_trial_params_["use_alternative_services"] = "true";
370 ConfigureQuicGlobals();
371 net::HttpNetworkSession::Params params;
372 InitializeNetworkSessionParams(&params);
373 EXPECT_TRUE(params.use_alternative_services);
376 TEST_F(IOThreadTest, QuicMaxNumberOfLossyConnectionsFieldTrialParams) {
377 field_trial_group_ = "Enabled";
378 field_trial_params_["max_number_of_lossy_connections"] = "5";
379 ConfigureQuicGlobals();
380 net::HttpNetworkSession::Params params;
381 InitializeNetworkSessionParams(&params);
382 EXPECT_EQ(5, params.quic_max_number_of_lossy_connections);
385 TEST_F(IOThreadTest, QuicPacketLossThresholdFieldTrialParams) {
386 field_trial_group_ = "Enabled";
387 field_trial_params_["packet_loss_threshold"] = "0.6";
388 ConfigureQuicGlobals();
389 net::HttpNetworkSession::Params params;
390 InitializeNetworkSessionParams(&params);
391 EXPECT_EQ(0.6f, params.quic_packet_loss_threshold);
394 TEST_F(IOThreadTest, QuicReceiveBufferSize) {
395 field_trial_group_ = "Enabled";
396 field_trial_params_["receive_buffer_size"] = "2097152";
397 ConfigureQuicGlobals();
398 net::HttpNetworkSession::Params params;
399 InitializeNetworkSessionParams(&params);
400 EXPECT_EQ(2097152, params.quic_socket_receive_buffer_size);
403 TEST_F(IOThreadTest, AlternativeServiceProbabilityThresholdFromFlag) {
404 command_line_.AppendSwitchASCII("alternative-service-probability-threshold",
405 "0.5");
407 ConfigureQuicGlobals();
408 net::HttpNetworkSession::Params params;
409 InitializeNetworkSessionParams(&params);
410 EXPECT_EQ(.5, params.alternative_service_probability_threshold);
413 TEST_F(IOThreadTest, AlternativeServiceProbabilityThresholdFromEnableQuicFlag) {
414 command_line_.AppendSwitch("enable-quic");
416 ConfigureQuicGlobals();
417 net::HttpNetworkSession::Params params;
418 InitializeNetworkSessionParams(&params);
419 EXPECT_EQ(0, params.alternative_service_probability_threshold);
422 // TODO(bnc): Remove when new parameter name rolls out and server configuration
423 // is changed.
424 TEST_F(IOThreadTest, AlternativeServiceProbabilityThresholdFromOldParams) {
425 field_trial_group_ = "Enabled";
426 field_trial_params_["alternate_protocol_probability_threshold"] = ".5";
428 ConfigureQuicGlobals();
429 net::HttpNetworkSession::Params params;
430 InitializeNetworkSessionParams(&params);
431 EXPECT_EQ(.5, params.alternative_service_probability_threshold);
434 TEST_F(IOThreadTest, AlternativeServiceProbabilityThresholdFromParams) {
435 field_trial_group_ = "Enabled";
436 field_trial_params_["alternative_service_probability_threshold"] = ".5";
438 ConfigureQuicGlobals();
439 net::HttpNetworkSession::Params params;
440 InitializeNetworkSessionParams(&params);
441 EXPECT_EQ(.5, params.alternative_service_probability_threshold);
444 TEST_F(IOThreadTest, QuicDisallowedByPolicy) {
445 command_line_.AppendSwitch(switches::kEnableQuic);
446 is_quic_allowed_by_policy_ = false;
447 ConfigureQuicGlobals();
449 net::HttpNetworkSession::Params params;
450 InitializeNetworkSessionParams(&params);
451 EXPECT_FALSE(params.enable_quic);
454 } // namespace test