[Password Manager] Do not partially deserialize FormData
[chromium-blink-merge.git] / chrome / browser / io_thread_unittest.cc
blobcb6e64984a9199046777dca52fdedd40e0cd0e80
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"
15 namespace test {
17 using ::testing::ElementsAre;
19 class BadEntropyProvider : public base::FieldTrial::EntropyProvider {
20 public:
21 ~BadEntropyProvider() override {}
23 double GetEntropyForTrial(const std::string& trial_name,
24 uint32 randomization_seed) const override {
25 return 0.5;
29 class IOThreadPeer {
30 public:
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 ConfigureSpdyGlobals(
41 const base::CommandLine& command_line,
42 base::StringPiece spdy_trial_group,
43 const std::map<std::string, std::string>& spdy_trial_params,
44 IOThread::Globals* globals) {
45 IOThread::ConfigureSpdyGlobals(command_line, spdy_trial_group,
46 spdy_trial_params, globals);
49 static void InitializeNetworkSessionParamsFromGlobals(
50 const IOThread::Globals& globals,
51 net::HttpNetworkSession::Params* params) {
52 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params);
56 class IOThreadTest : public testing::Test {
57 public:
58 IOThreadTest() : command_line_(base::CommandLine::NO_PROGRAM) {
59 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl());
62 void ConfigureQuicGlobals() {
63 IOThreadPeer::ConfigureQuicGlobals(command_line_, field_trial_group_,
64 field_trial_params_, &globals_);
67 void ConfigureSpdyGlobals() {
68 IOThreadPeer::ConfigureSpdyGlobals(command_line_, field_trial_group_,
69 field_trial_params_, &globals_);
72 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params) {
73 IOThreadPeer::InitializeNetworkSessionParamsFromGlobals(globals_, params);
76 base::CommandLine command_line_;
77 IOThread::Globals globals_;
78 std::string field_trial_group_;
79 std::map<std::string, std::string> field_trial_params_;
82 TEST_F(IOThreadTest, InitializeNetworkSessionParamsFromGlobals) {
83 globals_.quic_connection_options.push_back(net::kPACE);
84 globals_.quic_connection_options.push_back(net::kTBBR);
85 globals_.quic_connection_options.push_back(net::kTIME);
87 net::HttpNetworkSession::Params params;
88 InitializeNetworkSessionParams(&params);
89 EXPECT_EQ(globals_.quic_connection_options,
90 params.quic_connection_options);
93 TEST_F(IOThreadTest, SpdyFieldTrialHoldbackEnabled) {
94 net::HttpStreamFactory::set_spdy_enabled(true);
95 field_trial_group_ = "SpdyDisabled";
96 ConfigureSpdyGlobals();
97 EXPECT_FALSE(net::HttpStreamFactory::spdy_enabled());
100 TEST_F(IOThreadTest, SpdyFieldTrialSpdy31Enabled) {
101 bool use_alternate_protocols = false;
102 field_trial_group_ = "Spdy31Enabled";
103 ConfigureSpdyGlobals();
104 EXPECT_THAT(globals_.next_protos,
105 ElementsAre(net::kProtoHTTP11,
106 net::kProtoSPDY31));
107 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
108 EXPECT_TRUE(use_alternate_protocols);
111 TEST_F(IOThreadTest, SpdyFieldTrialSpdy4Enabled) {
112 bool use_alternate_protocols = false;
113 field_trial_group_ = "Spdy4Enabled";
114 ConfigureSpdyGlobals();
115 EXPECT_THAT(globals_.next_protos,
116 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
117 net::kProtoSPDY4_14, net::kProtoSPDY4));
118 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
119 EXPECT_TRUE(use_alternate_protocols);
122 TEST_F(IOThreadTest, SpdyFieldTrialDefault) {
123 field_trial_group_ = "";
124 ConfigureSpdyGlobals();
125 EXPECT_THAT(globals_.next_protos,
126 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
127 net::kProtoSPDY4_14, net::kProtoSPDY4));
128 bool use_alternate_protocols = false;
129 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
130 EXPECT_TRUE(use_alternate_protocols);
133 TEST_F(IOThreadTest, SpdyFieldTrialParametrized) {
134 field_trial_params_["enable_spdy31"] = "false";
135 // Undefined parameter "enable_http2_14" should default to false.
136 field_trial_params_["enable_http2"] = "true";
137 field_trial_group_ = "ParametrizedHTTP2Only";
138 ConfigureSpdyGlobals();
139 EXPECT_THAT(globals_.next_protos,
140 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY4));
141 bool use_alternate_protocols = false;
142 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
143 EXPECT_TRUE(use_alternate_protocols);
146 TEST_F(IOThreadTest, SpdyCommandLineEnable) {
147 command_line_.AppendSwitch("enable-spdy4");
148 // Command line should overwrite field trial group.
149 field_trial_group_ = "SpdyDisabled";
150 ConfigureSpdyGlobals();
151 EXPECT_THAT(globals_.next_protos,
152 ElementsAre(net::kProtoHTTP11, net::kProtoSPDY31,
153 net::kProtoSPDY4_14, net::kProtoSPDY4));
154 bool use_alternate_protocols = false;
155 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
156 EXPECT_TRUE(use_alternate_protocols);
159 TEST_F(IOThreadTest, SpdyCommandLineDisable) {
160 command_line_.AppendSwitch("enable-npn-http");
161 // Command line should overwrite field trial group.
162 field_trial_group_ = "Spdy4Enabled";
163 ConfigureSpdyGlobals();
164 EXPECT_THAT(globals_.next_protos, ElementsAre(net::kProtoHTTP11));
165 bool use_alternate_protocols = true;
166 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
167 EXPECT_FALSE(use_alternate_protocols);
170 TEST_F(IOThreadTest, SpdyCommandLineUseSpdyOff) {
171 command_line_.AppendSwitchASCII("use-spdy", "off");
172 // Command line should overwrite field trial group.
173 field_trial_group_ = "Spdy4Enabled";
174 ConfigureSpdyGlobals();
175 EXPECT_EQ(0u, globals_.next_protos.size());
178 TEST_F(IOThreadTest, SpdyCommandLineUseSpdySSL) {
179 command_line_.AppendSwitchASCII("use-spdy", "ssl");
180 // Command line should overwrite field trial group.
181 field_trial_group_ = "SpdyDisabled";
182 ConfigureSpdyGlobals();
183 bool force_spdy_over_ssl = false;
184 globals_.force_spdy_over_ssl.CopyToIfSet(&force_spdy_over_ssl);
185 EXPECT_TRUE(force_spdy_over_ssl);
186 bool force_spdy_always = false;
187 globals_.force_spdy_always.CopyToIfSet(&force_spdy_always);
188 EXPECT_TRUE(force_spdy_always);
191 TEST_F(IOThreadTest, SpdyCommandLineUseSpdyDisableAltProtocols) {
192 command_line_.AppendSwitchASCII("use-spdy", "no-alt-protocols");
193 ConfigureSpdyGlobals();
194 bool use_alternate_protocols = true;
195 globals_.use_alternate_protocols.CopyToIfSet(&use_alternate_protocols);
196 EXPECT_FALSE(use_alternate_protocols);
199 TEST_F(IOThreadTest, DisableQuicByDefault) {
200 ConfigureQuicGlobals();
201 net::HttpNetworkSession::Params params;
202 InitializeNetworkSessionParams(&params);
203 EXPECT_FALSE(params.enable_quic);
204 EXPECT_FALSE(params.enable_quic_for_proxies);
205 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
208 TEST_F(IOThreadTest, EnableQuicFromFieldTrialGroup) {
209 field_trial_group_ = "Enabled";
211 ConfigureQuicGlobals();
212 net::HttpNetworkSession::Params default_params;
213 net::HttpNetworkSession::Params params;
214 InitializeNetworkSessionParams(&params);
215 EXPECT_TRUE(params.enable_quic);
216 EXPECT_TRUE(params.enable_quic_for_proxies);
217 EXPECT_EQ(1350u, params.quic_max_packet_length);
218 EXPECT_EQ(1.0, params.alternate_protocol_probability_threshold);
219 EXPECT_EQ(default_params.quic_supported_versions,
220 params.quic_supported_versions);
221 EXPECT_EQ(net::QuicTagVector(), params.quic_connection_options);
222 EXPECT_FALSE(params.quic_always_require_handshake_confirmation);
223 EXPECT_FALSE(params.quic_disable_connection_pooling);
224 EXPECT_EQ(0.25f, params.quic_load_server_info_timeout_srtt_multiplier);
225 EXPECT_FALSE(params.quic_enable_connection_racing);
226 EXPECT_FALSE(params.quic_enable_non_blocking_io);
227 EXPECT_FALSE(params.quic_disable_disk_cache);
228 EXPECT_EQ(0, params.quic_max_number_of_lossy_connections);
229 EXPECT_EQ(1.0f, params.quic_packet_loss_threshold);
230 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
233 TEST_F(IOThreadTest, EnableQuicFromQuicProxyFieldTrialGroup) {
234 base::FieldTrialList field_trial_list(new BadEntropyProvider());
235 base::FieldTrialList::CreateFieldTrial(
236 data_reduction_proxy::DataReductionProxyParams::GetQuicFieldTrialName(),
237 "Enabled");
239 ConfigureQuicGlobals();
240 net::HttpNetworkSession::Params params;
241 InitializeNetworkSessionParams(&params);
242 EXPECT_FALSE(params.enable_quic);
243 EXPECT_TRUE(params.enable_quic_for_proxies);
244 EXPECT_TRUE(IOThread::ShouldEnableQuicForDataReductionProxy());
245 EXPECT_EQ(256 * 1024, params.quic_socket_receive_buffer_size);
248 TEST_F(IOThreadTest, EnableQuicFromCommandLine) {
249 command_line_.AppendSwitch("enable-quic");
251 ConfigureQuicGlobals();
252 net::HttpNetworkSession::Params params;
253 InitializeNetworkSessionParams(&params);
254 EXPECT_TRUE(params.enable_quic);
255 EXPECT_TRUE(params.enable_quic_for_proxies);
256 EXPECT_FALSE(IOThread::ShouldEnableQuicForDataReductionProxy());
259 TEST_F(IOThreadTest, EnablePacingFromCommandLine) {
260 command_line_.AppendSwitch("enable-quic");
261 command_line_.AppendSwitch("enable-quic-pacing");
263 ConfigureQuicGlobals();
264 net::HttpNetworkSession::Params params;
265 InitializeNetworkSessionParams(&params);
266 net::QuicTagVector options;
267 options.push_back(net::kPACE);
268 EXPECT_EQ(options, params.quic_connection_options);
271 TEST_F(IOThreadTest, EnablePacingFromFieldTrialParams) {
272 field_trial_group_ = "Enabled";
273 field_trial_params_["enable_pacing"] = "true";
275 ConfigureQuicGlobals();
276 net::HttpNetworkSession::Params params;
277 InitializeNetworkSessionParams(&params);
278 net::QuicTagVector options;
279 options.push_back(net::kPACE);
280 EXPECT_EQ(options, params.quic_connection_options);
283 TEST_F(IOThreadTest, PacketLengthFromCommandLine) {
284 command_line_.AppendSwitch("enable-quic");
285 command_line_.AppendSwitchASCII("quic-max-packet-length", "1450");
287 ConfigureQuicGlobals();
288 net::HttpNetworkSession::Params params;
289 InitializeNetworkSessionParams(&params);
290 EXPECT_EQ(1450u, params.quic_max_packet_length);
293 TEST_F(IOThreadTest, PacketLengthFromFieldTrialParams) {
294 field_trial_group_ = "Enabled";
295 field_trial_params_["max_packet_length"] = "1450";
297 ConfigureQuicGlobals();
298 net::HttpNetworkSession::Params params;
299 InitializeNetworkSessionParams(&params);
300 EXPECT_EQ(1450u, params.quic_max_packet_length);
303 TEST_F(IOThreadTest, QuicVersionFromCommandLine) {
304 command_line_.AppendSwitch("enable-quic");
305 std::string version =
306 net::QuicVersionToString(net::QuicSupportedVersions().back());
307 command_line_.AppendSwitchASCII("quic-version", version);
309 ConfigureQuicGlobals();
310 net::HttpNetworkSession::Params params;
311 InitializeNetworkSessionParams(&params);
312 net::QuicVersionVector supported_versions;
313 supported_versions.push_back(net::QuicSupportedVersions().back());
314 EXPECT_EQ(supported_versions, params.quic_supported_versions);
317 TEST_F(IOThreadTest, QuicVersionFromFieldTrialParams) {
318 field_trial_group_ = "Enabled";
319 field_trial_params_["quic_version"] =
320 net::QuicVersionToString(net::QuicSupportedVersions().back());
322 ConfigureQuicGlobals();
323 net::HttpNetworkSession::Params params;
324 InitializeNetworkSessionParams(&params);
325 net::QuicVersionVector supported_versions;
326 supported_versions.push_back(net::QuicSupportedVersions().back());
327 EXPECT_EQ(supported_versions, params.quic_supported_versions);
330 TEST_F(IOThreadTest, QuicConnectionOptionsFromCommandLine) {
331 command_line_.AppendSwitch("enable-quic");
332 command_line_.AppendSwitchASCII("quic-connection-options",
333 "PACE,TIME,TBBR,REJ");
335 ConfigureQuicGlobals();
336 net::HttpNetworkSession::Params params;
337 InitializeNetworkSessionParams(&params);
339 net::QuicTagVector options;
340 options.push_back(net::kPACE);
341 options.push_back(net::kTIME);
342 options.push_back(net::kTBBR);
343 options.push_back(net::kREJ);
344 EXPECT_EQ(options, params.quic_connection_options);
347 TEST_F(IOThreadTest, QuicConnectionOptionsFromFieldTrialParams) {
348 field_trial_group_ = "Enabled";
349 field_trial_params_["connection_options"] = "PACE,TIME,TBBR,REJ";
351 ConfigureQuicGlobals();
352 net::HttpNetworkSession::Params params;
353 InitializeNetworkSessionParams(&params);
355 net::QuicTagVector options;
356 options.push_back(net::kPACE);
357 options.push_back(net::kTIME);
358 options.push_back(net::kTBBR);
359 options.push_back(net::kREJ);
360 EXPECT_EQ(options, params.quic_connection_options);
363 TEST_F(IOThreadTest,
364 QuicAlwaysRequireHandshakeConfirmationFromFieldTrialParams) {
365 field_trial_group_ = "Enabled";
366 field_trial_params_["always_require_handshake_confirmation"] = "true";
367 ConfigureQuicGlobals();
368 net::HttpNetworkSession::Params params;
369 InitializeNetworkSessionParams(&params);
370 EXPECT_TRUE(params.quic_always_require_handshake_confirmation);
373 TEST_F(IOThreadTest,
374 QuicDisableConnectionPoolingFromFieldTrialParams) {
375 field_trial_group_ = "Enabled";
376 field_trial_params_["disable_connection_pooling"] = "true";
377 ConfigureQuicGlobals();
378 net::HttpNetworkSession::Params params;
379 InitializeNetworkSessionParams(&params);
380 EXPECT_TRUE(params.quic_disable_connection_pooling);
383 TEST_F(IOThreadTest, QuicLoadServerInfoTimeToSmoothedRttFromFieldTrialParams) {
384 field_trial_group_ = "Enabled";
385 field_trial_params_["load_server_info_time_to_srtt"] = "0.5";
386 ConfigureQuicGlobals();
387 net::HttpNetworkSession::Params params;
388 InitializeNetworkSessionParams(&params);
389 EXPECT_EQ(0.5f, params.quic_load_server_info_timeout_srtt_multiplier);
392 TEST_F(IOThreadTest, QuicEnableConnectionRacing) {
393 field_trial_group_ = "Enabled";
394 field_trial_params_["enable_connection_racing"] = "true";
395 ConfigureQuicGlobals();
396 net::HttpNetworkSession::Params params;
397 InitializeNetworkSessionParams(&params);
398 EXPECT_TRUE(params.quic_enable_connection_racing);
401 TEST_F(IOThreadTest, QuicEnableNonBlockingIO) {
402 field_trial_group_ = "Enabled";
403 field_trial_params_["enable_non_blocking_io"] = "true";
404 ConfigureQuicGlobals();
405 net::HttpNetworkSession::Params params;
406 InitializeNetworkSessionParams(&params);
407 EXPECT_TRUE(params.quic_enable_non_blocking_io);
410 TEST_F(IOThreadTest, QuicDisableDiskCache) {
411 field_trial_group_ = "Enabled";
412 field_trial_params_["disable_disk_cache"] = "true";
413 ConfigureQuicGlobals();
414 net::HttpNetworkSession::Params params;
415 InitializeNetworkSessionParams(&params);
416 EXPECT_TRUE(params.quic_disable_disk_cache);
419 TEST_F(IOThreadTest, QuicMaxNumberOfLossyConnectionsFieldTrialParams) {
420 field_trial_group_ = "Enabled";
421 field_trial_params_["max_number_of_lossy_connections"] = "5";
422 ConfigureQuicGlobals();
423 net::HttpNetworkSession::Params params;
424 InitializeNetworkSessionParams(&params);
425 EXPECT_EQ(5, params.quic_max_number_of_lossy_connections);
428 TEST_F(IOThreadTest, QuicPacketLossThresholdFieldTrialParams) {
429 field_trial_group_ = "Enabled";
430 field_trial_params_["packet_loss_threshold"] = "0.5";
431 ConfigureQuicGlobals();
432 net::HttpNetworkSession::Params params;
433 InitializeNetworkSessionParams(&params);
434 EXPECT_EQ(0.5f, params.quic_packet_loss_threshold);
437 TEST_F(IOThreadTest, QuicReceiveBufferSize) {
438 field_trial_group_ = "Enabled";
439 field_trial_params_["receive_buffer_size"] = "1048576";
440 ConfigureQuicGlobals();
441 net::HttpNetworkSession::Params params;
442 InitializeNetworkSessionParams(&params);
443 EXPECT_EQ(1048576, params.quic_socket_receive_buffer_size);
446 TEST_F(IOThreadTest,
447 AlternateProtocolProbabilityThresholdFromFlag) {
448 command_line_.AppendSwitchASCII("alternate-protocol-probability-threshold",
449 ".5");
451 ConfigureQuicGlobals();
452 net::HttpNetworkSession::Params params;
453 InitializeNetworkSessionParams(&params);
454 EXPECT_EQ(.5, params.alternate_protocol_probability_threshold);
457 TEST_F(IOThreadTest,
458 AlternateProtocolProbabilityThresholdFromEnableQuicFlag) {
459 command_line_.AppendSwitch("enable-quic");
461 ConfigureQuicGlobals();
462 net::HttpNetworkSession::Params params;
463 InitializeNetworkSessionParams(&params);
464 EXPECT_EQ(0, params.alternate_protocol_probability_threshold);
467 TEST_F(IOThreadTest,
468 AlternateProtocolProbabilityThresholdFromParams) {
469 field_trial_group_ = "Enabled";
470 field_trial_params_["alternate_protocol_probability_threshold"] = ".5";
472 ConfigureQuicGlobals();
473 net::HttpNetworkSession::Params params;
474 InitializeNetworkSessionParams(&params);
475 EXPECT_EQ(.5, params.alternate_protocol_probability_threshold);
478 } // namespace test