• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/cronet/url_request_context_config.h"
6 
7 #include <memory>
8 
9 #include "base/check.h"
10 #include "base/containers/contains.h"
11 #include "base/feature_list.h"
12 #include "base/functional/bind.h"
13 #include "base/json/json_writer.h"
14 #include "base/notreached.h"
15 #include "base/strings/strcat.h"
16 #include "base/strings/string_piece.h"
17 #include "base/test/task_environment.h"
18 #include "base/test/values_test_util.h"
19 #include "base/values.h"
20 #include "build/build_config.h"
21 #include "net/base/features.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/base/http_user_agent_settings.h"
24 #include "net/base/net_errors.h"
25 #include "net/base/network_isolation_key.h"
26 #include "net/cert/cert_verifier.h"
27 #include "net/dns/host_resolver.h"
28 #include "net/dns/host_resolver_manager.h"
29 #include "net/http/http_network_session.h"
30 #include "net/log/net_log_with_source.h"
31 #include "net/proxy_resolution/proxy_config.h"
32 #include "net/proxy_resolution/proxy_config_service_fixed.h"
33 #include "net/url_request/url_request_context.h"
34 #include "net/url_request/url_request_context_builder.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 
37 #if BUILDFLAG(ENABLE_REPORTING)
38 #include "net/network_error_logging/network_error_logging_service.h"
39 #include "net/reporting/reporting_service.h"
40 #endif  // BUILDFLAG(ENABLE_REPORTING)
41 
42 namespace cronet {
43 
44 namespace {
45 
WrapJsonHeader(base::StringPiece value)46 std::string WrapJsonHeader(base::StringPiece value) {
47   return base::StrCat({"[", value, "]"});
48 }
49 
50 // Returns whether two JSON-encoded headers contain the same content, ignoring
51 // irrelevant encoding issues like whitespace and map element ordering.
JsonHeaderEquals(base::StringPiece expected,base::StringPiece actual)52 bool JsonHeaderEquals(base::StringPiece expected, base::StringPiece actual) {
53   return base::test::ParseJson(WrapJsonHeader(expected)) ==
54          base::test::ParseJson(WrapJsonHeader(actual));
55 }
56 
57 }  // namespace
58 
TEST(URLRequestContextConfigTest,TestExperimentalOptionParsing)59 TEST(URLRequestContextConfigTest, TestExperimentalOptionParsing) {
60   base::test::TaskEnvironment task_environment_(
61       base::test::TaskEnvironment::MainThreadType::IO);
62 
63   // Create JSON for experimental options.
64   base::Value::Dict options;
65   options.SetByDottedPath("QUIC.max_server_configs_stored_in_properties", 2);
66   options.SetByDottedPath("QUIC.idle_connection_timeout_seconds", 300);
67   options.SetByDottedPath("QUIC.close_sessions_on_ip_change", true);
68   options.SetByDottedPath("QUIC.connection_options", "TIME,TBBR,REJ");
69   options.SetByDottedPath(
70       "QUIC.set_quic_flags",
71       "FLAGS_quic_reloadable_flag_quic_testonly_default_false=true,"
72       "FLAGS_quic_restart_flag_quic_testonly_default_true=false");
73   options.SetByDottedPath("AsyncDNS.enable", true);
74   options.SetByDottedPath("NetworkErrorLogging.enable", true);
75   options.SetByDottedPath("NetworkErrorLogging.preloaded_report_to_headers",
76                           base::test::ParseJson(R"json(
77                   [
78                     {
79                       "origin": "https://test-origin/",
80                       "value": {
81                         "group": "test-group",
82                         "max_age": 86400,
83                         "endpoints": [
84                           {"url": "https://test-endpoint/"},
85                         ],
86                       },
87                     },
88                     {
89                       "origin": "https://test-origin-2/",
90                       "value": [
91                         {
92                           "group": "test-group-2",
93                           "max_age": 86400,
94                           "endpoints": [
95                             {"url": "https://test-endpoint-2/"},
96                           ],
97                         },
98                         {
99                           "group": "test-group-3",
100                           "max_age": 86400,
101                           "endpoints": [
102                             {"url": "https://test-endpoint-3/"},
103                           ],
104                         },
105                       ],
106                     },
107                     {
108                       "origin": "https://value-is-missing/",
109                     },
110                     {
111                       "value": "origin is missing",
112                     },
113                     {
114                       "origin": 123,
115                       "value": "origin is not a string",
116                     },
117                     {
118                       "origin": "this is not a URL",
119                       "value": "origin not a URL",
120                     },
121                   ]
122                   )json"));
123   options.SetByDottedPath("NetworkErrorLogging.preloaded_nel_headers",
124                           base::test::ParseJson(R"json(
125                   [
126                     {
127                       "origin": "https://test-origin/",
128                       "value": {
129                         "report_to": "test-group",
130                         "max_age": 86400,
131                       },
132                     },
133                   ]
134                   )json"));
135   options.SetByDottedPath("UnknownOption.foo", true);
136   options.SetByDottedPath("HostResolverRules.host_resolver_rules",
137                           "MAP * 127.0.0.1");
138   // See http://crbug.com/696569.
139   options.Set("disable_ipv6_on_wifi", true);
140   options.Set("spdy_go_away_on_ip_change", true);
141   std::string options_json;
142   EXPECT_TRUE(base::JSONWriter::Write(options, &options_json));
143 
144   // Initialize QUIC flags set by the config.
145   FLAGS_quic_reloadable_flag_quic_testonly_default_false = false;
146   FLAGS_quic_restart_flag_quic_testonly_default_true = true;
147 
148   std::unique_ptr<URLRequestContextConfig> config =
149       URLRequestContextConfig::CreateURLRequestContextConfig(
150           // Enable QUIC.
151           true,
152           // Enable SPDY.
153           true,
154           // Enable Brotli.
155           false,
156           // Type of http cache.
157           URLRequestContextConfig::HttpCacheType::DISK,
158           // Max size of http cache in bytes.
159           1024000,
160           // Disable caching for HTTP responses. Other information may be stored
161           // in the cache.
162           false,
163           // Storage path for http cache and cookie storage.
164           "/data/data/org.chromium.net/app_cronet_test/test_storage",
165           // Accept-Language request header field.
166           "foreign-language",
167           // User-Agent request header field.
168           "fake agent",
169           // JSON encoded experimental options.
170           options_json,
171           // MockCertVerifier to use for testing purposes.
172           std::unique_ptr<net::CertVerifier>(),
173           // Enable network quality estimator.
174           false,
175           // Enable Public Key Pinning bypass for local trust anchors.
176           true,
177           // Optional network thread priority.
178           absl::optional<double>(42.0));
179 
180   net::URLRequestContextBuilder builder;
181   config->ConfigureURLRequestContextBuilder(&builder);
182   EXPECT_FALSE(
183       config->effective_experimental_options.contains("UnknownOption"));
184   // Set a ProxyConfigService to avoid DCHECK failure when building.
185   builder.set_proxy_config_service(
186       std::make_unique<net::ProxyConfigServiceFixed>(
187           net::ProxyConfigWithAnnotation::CreateDirect()));
188   std::unique_ptr<net::URLRequestContext> context(builder.Build());
189   const net::QuicParams* quic_params = context->quic_context()->params();
190   // Check Quic Connection options.
191   quic::QuicTagVector quic_connection_options;
192   quic_connection_options.push_back(quic::kTIME);
193   quic_connection_options.push_back(quic::kTBBR);
194   quic_connection_options.push_back(quic::kREJ);
195   EXPECT_EQ(quic_connection_options, quic_params->connection_options);
196 
197   // Check QUIC flags.
198   EXPECT_TRUE(FLAGS_quic_reloadable_flag_quic_testonly_default_false);
199   EXPECT_FALSE(FLAGS_quic_restart_flag_quic_testonly_default_true);
200 
201   // Check max_server_configs_stored_in_properties.
202   EXPECT_EQ(2u, quic_params->max_server_configs_stored_in_properties);
203 
204   // Check idle_connection_timeout.
205   EXPECT_EQ(300, quic_params->idle_connection_timeout.InSeconds());
206 
207   EXPECT_TRUE(quic_params->close_sessions_on_ip_change);
208   EXPECT_FALSE(quic_params->goaway_sessions_on_ip_change);
209   EXPECT_FALSE(quic_params->allow_server_migration);
210   EXPECT_FALSE(quic_params->migrate_sessions_on_network_change_v2);
211   EXPECT_FALSE(quic_params->migrate_sessions_early_v2);
212   EXPECT_FALSE(quic_params->migrate_idle_sessions);
213   EXPECT_FALSE(quic_params->retry_on_alternate_network_before_handshake);
214   EXPECT_TRUE(quic_params->allow_port_migration);
215   EXPECT_FALSE(quic_params->disable_tls_zero_rtt);
216   EXPECT_TRUE(quic_params->retry_without_alt_svc_on_quic_errors);
217   EXPECT_FALSE(
218       quic_params->initial_delay_for_broken_alternative_service.has_value());
219   EXPECT_FALSE(quic_params->exponential_backoff_on_initial_delay.has_value());
220   EXPECT_FALSE(quic_params->delay_main_job_with_available_spdy_session);
221 
222 #if defined(ENABLE_BUILT_IN_DNS)
223   // Check AsyncDNS resolver is enabled.
224   EXPECT_TRUE(context->host_resolver()->GetDnsConfigAsValue());
225 #endif  // defined(ENABLE_BUILT_IN_DNS)
226 
227 #if BUILDFLAG(ENABLE_REPORTING)
228   // Check Reporting and Network Error Logging are enabled (can be disabled at
229   // build time).
230   EXPECT_TRUE(context->reporting_service());
231   EXPECT_TRUE(context->network_error_logging_service());
232 #endif  // BUILDFLAG(ENABLE_REPORTING)
233 
234   ASSERT_EQ(2u, config->preloaded_report_to_headers.size());
235   EXPECT_EQ(url::Origin::CreateFromNormalizedTuple("https", "test-origin", 443),
236             config->preloaded_report_to_headers[0].origin);
237   EXPECT_TRUE(JsonHeaderEquals(  //
238       R"json(
239       {
240         "group": "test-group",
241         "max_age": 86400,
242         "endpoints": [
243           {"url": "https://test-endpoint/"},
244         ],
245       }
246       )json",
247       config->preloaded_report_to_headers[0].value));
248   EXPECT_EQ(
249       url::Origin::CreateFromNormalizedTuple("https", "test-origin-2", 443),
250       config->preloaded_report_to_headers[1].origin);
251   EXPECT_TRUE(JsonHeaderEquals(  //
252       R"json(
253       {
254         "group": "test-group-2",
255         "max_age": 86400,
256         "endpoints": [
257           {"url": "https://test-endpoint-2/"},
258         ],
259       },
260       {
261         "group": "test-group-3",
262         "max_age": 86400,
263         "endpoints": [
264           {"url": "https://test-endpoint-3/"},
265         ],
266       }
267       )json",
268       config->preloaded_report_to_headers[1].value));
269 
270   ASSERT_EQ(1u, config->preloaded_nel_headers.size());
271   EXPECT_EQ(url::Origin::CreateFromNormalizedTuple("https", "test-origin", 443),
272             config->preloaded_nel_headers[0].origin);
273   EXPECT_TRUE(JsonHeaderEquals(  //
274       R"json(
275       {
276         "report_to": "test-group",
277         "max_age": 86400,
278       }
279       )json",
280       config->preloaded_nel_headers[0].value));
281 
282   // Check IPv6 is disabled when on wifi.
283   EXPECT_FALSE(context->host_resolver()
284                    ->GetManagerForTesting()
285                    ->check_ipv6_on_wifi_for_testing());
286 
287   const net::HttpNetworkSessionParams* params =
288       context->GetNetworkSessionParams();
289   EXPECT_TRUE(params->spdy_go_away_on_ip_change);
290 
291   // All host resolution expected to be mapped to an immediately-resolvable IP.
292   std::unique_ptr<net::HostResolver::ResolveHostRequest> resolve_request =
293       context->host_resolver()->CreateRequest(
294           net::HostPortPair("abcde", 80), net::NetworkAnonymizationKey(),
295           net::NetLogWithSource(), absl::nullopt);
296   EXPECT_EQ(net::OK, resolve_request->Start(
__anon1f2149d50202(int error) 297                          base::BindOnce([](int error) { NOTREACHED(); })));
298 
299   EXPECT_TRUE(config->network_thread_priority);
300   EXPECT_EQ(42.0, config->network_thread_priority.value());
301   EXPECT_FALSE(config->bidi_stream_detect_broken_connection);
302 
303   // When UseDnsHttpsSvcb option is not set, the value of net::features are
304   // used.
305   const net::HostResolver::HttpsSvcbOptions& https_svcb_options =
306       context->host_resolver()
307           ->GetManagerForTesting()
308           ->https_svcb_options_for_testing();
309   EXPECT_EQ(base::FeatureList::IsEnabled(net::features::kUseDnsHttpsSvcb),
310             https_svcb_options.enable);
311   EXPECT_EQ(net::features::kUseDnsHttpsSvcbInsecureExtraTimeMax.Get(),
312             https_svcb_options.insecure_extra_time_max);
313   EXPECT_EQ(net::features::kUseDnsHttpsSvcbInsecureExtraTimePercent.Get(),
314             https_svcb_options.insecure_extra_time_percent);
315   EXPECT_EQ(net::features::kUseDnsHttpsSvcbInsecureExtraTimeMin.Get(),
316             https_svcb_options.insecure_extra_time_min);
317   EXPECT_EQ(net::features::kUseDnsHttpsSvcbSecureExtraTimeMax.Get(),
318             https_svcb_options.secure_extra_time_max);
319   EXPECT_EQ(net::features::kUseDnsHttpsSvcbSecureExtraTimePercent.Get(),
320             https_svcb_options.secure_extra_time_percent);
321   EXPECT_EQ(net::features::kUseDnsHttpsSvcbSecureExtraTimeMin.Get(),
322             https_svcb_options.secure_extra_time_min);
323   EXPECT_EQ(base::FeatureList::IsEnabled(net::features::kUseDnsHttpsSvcbAlpn),
324             params->use_dns_https_svcb_alpn);
325 
326   EXPECT_TRUE(config->enable_telemetry);
327 }
328 
329 TEST(URLRequestContextConfigTest, SetSupportedQuicVersionByAlpn) {
330   base::test::TaskEnvironment task_environment_(
331       base::test::TaskEnvironment::MainThreadType::IO);
332 
333   quic::ParsedQuicVersion version = quic::AllSupportedVersions().front();
334   std::string experimental_options = "{\"QUIC\":{\"quic_version\":\"" +
335                                      quic::ParsedQuicVersionToString(version) +
336                                      "\"}}";
337 
338   std::unique_ptr<URLRequestContextConfig> config =
339       URLRequestContextConfig::CreateURLRequestContextConfig(
340           // Enable QUIC.
341           true,
342           // Enable SPDY.
343           true,
344           // Enable Brotli.
345           false,
346           // Type of http cache.
347           URLRequestContextConfig::HttpCacheType::DISK,
348           // Max size of http cache in bytes.
349           1024000,
350           // Disable caching for HTTP responses. Other information may be stored
351           // in the cache.
352           false,
353           // Storage path for http cache and cookie storage.
354           "/data/data/org.chromium.net/app_cronet_test/test_storage",
355           // Accept-Language request header field.
356           "foreign-language",
357           // User-Agent request header field.
358           "fake agent",
359           // JSON encoded experimental options.
360           experimental_options,
361           // MockCertVerifier to use for testing purposes.
362           std::unique_ptr<net::CertVerifier>(),
363           // Enable network quality estimator.
364           false,
365           // Enable Public Key Pinning bypass for local trust anchors.
366           true,
367           // Optional network thread priority.
368           absl::optional<double>());
369 
370   net::URLRequestContextBuilder builder;
371   config->ConfigureURLRequestContextBuilder(&builder);
372   // Set a ProxyConfigService to avoid DCHECK failure when building.
373   builder.set_proxy_config_service(
374       std::make_unique<net::ProxyConfigServiceFixed>(
375           net::ProxyConfigWithAnnotation::CreateDirect()));
376   std::unique_ptr<net::URLRequestContext> context(builder.Build());
377   const net::QuicParams* quic_params = context->quic_context()->params();
378   EXPECT_EQ(quic_params->supported_versions.size(), 1u);
379   EXPECT_EQ(quic_params->supported_versions[0], version);
380 }
381 
TEST(URLRequestContextConfigTest,SetUnsupportedQuicVersion)382 TEST(URLRequestContextConfigTest, SetUnsupportedQuicVersion) {
383   base::test::TaskEnvironment task_environment_(
384       base::test::TaskEnvironment::MainThreadType::IO);
385 
386   std::unique_ptr<URLRequestContextConfig> config =
387       URLRequestContextConfig::CreateURLRequestContextConfig(
388           // Enable QUIC.
389           true,
390           // Enable SPDY.
391           true,
392           // Enable Brotli.
393           false,
394           // Type of http cache.
395           URLRequestContextConfig::HttpCacheType::DISK,
396           // Max size of http cache in bytes.
397           1024000,
398           // Disable caching for HTTP responses. Other information may be stored
399           // in the cache.
400           false,
401           // Storage path for http cache and cookie storage.
402           "/data/data/org.chromium.net/app_cronet_test/test_storage",
403           // Accept-Language request header field.
404           "foreign-language",
405           // User-Agent request header field.
406           "fake agent",
407           // JSON encoded experimental options.
408           "{\"QUIC\":{\"quic_version\":\"h3-Q047\"}}",
409           // MockCertVerifier to use for testing purposes.
410           std::unique_ptr<net::CertVerifier>(),
411           // Enable network quality estimator.
412           false,
413           // Enable Public Key Pinning bypass for local trust anchors.
414           true,
415           // Optional network thread priority.
416           absl::optional<double>());
417 
418   net::URLRequestContextBuilder builder;
419   config->ConfigureURLRequestContextBuilder(&builder);
420   // Set a ProxyConfigService to avoid DCHECK failure when building.
421   builder.set_proxy_config_service(
422       std::make_unique<net::ProxyConfigServiceFixed>(
423           net::ProxyConfigWithAnnotation::CreateDirect()));
424   std::unique_ptr<net::URLRequestContext> context(builder.Build());
425   const net::QuicParams* quic_params = context->quic_context()->params();
426   EXPECT_EQ(quic_params->supported_versions,
427             net::DefaultSupportedQuicVersions());
428 }
429 
TEST(URLRequestContextConfigTest,SetObsoleteQuicVersion)430 TEST(URLRequestContextConfigTest, SetObsoleteQuicVersion) {
431   // This test configures cronet with an obsolete QUIC version and validates
432   // that cronet ignores that version and uses the default versions.
433   base::test::TaskEnvironment task_environment_(
434       base::test::TaskEnvironment::MainThreadType::IO);
435 
436   std::unique_ptr<URLRequestContextConfig> config =
437       URLRequestContextConfig::CreateURLRequestContextConfig(
438           // Enable QUIC.
439           true,
440           // Enable SPDY.
441           true,
442           // Enable Brotli.
443           false,
444           // Type of http cache.
445           URLRequestContextConfig::HttpCacheType::DISK,
446           // Max size of http cache in bytes.
447           1024000,
448           // Disable caching for HTTP responses. Other information may be stored
449           // in the cache.
450           false,
451           // Storage path for http cache and cookie storage.
452           "/data/data/org.chromium.net/app_cronet_test/test_storage",
453           // Accept-Language request header field.
454           "foreign-language",
455           // User-Agent request header field.
456           "fake agent",
457           // JSON encoded experimental options.
458           std::string("{\"QUIC\":{\"quic_version\":\"") +
459               quic::ParsedQuicVersionToString(
460                   net::ObsoleteQuicVersions().back()) +
461               "\"}}",
462           // MockCertVerifier to use for testing purposes.
463           std::unique_ptr<net::CertVerifier>(),
464           // Enable network quality estimator.
465           false,
466           // Enable Public Key Pinning bypass for local trust anchors.
467           true,
468           // Optional network thread priority.
469           absl::optional<double>());
470 
471   net::URLRequestContextBuilder builder;
472   config->ConfigureURLRequestContextBuilder(&builder);
473   // Set a ProxyConfigService to avoid DCHECK failure when building.
474   builder.set_proxy_config_service(
475       std::make_unique<net::ProxyConfigServiceFixed>(
476           net::ProxyConfigWithAnnotation::CreateDirect()));
477   std::unique_ptr<net::URLRequestContext> context(builder.Build());
478   const net::QuicParams* quic_params = context->quic_context()->params();
479   EXPECT_EQ(quic_params->supported_versions,
480             net::DefaultSupportedQuicVersions());
481 }
482 
TEST(URLRequestContextConfigTest,SetQuicServerMigrationOptions)483 TEST(URLRequestContextConfigTest, SetQuicServerMigrationOptions) {
484   base::test::TaskEnvironment task_environment_(
485       base::test::TaskEnvironment::MainThreadType::IO);
486 
487   std::unique_ptr<URLRequestContextConfig> config =
488       URLRequestContextConfig::CreateURLRequestContextConfig(
489           // Enable QUIC.
490           true,
491           // Enable SPDY.
492           true,
493           // Enable Brotli.
494           false,
495           // Type of http cache.
496           URLRequestContextConfig::HttpCacheType::DISK,
497           // Max size of http cache in bytes.
498           1024000,
499           // Disable caching for HTTP responses. Other information may be stored
500           // in the cache.
501           false,
502           // Storage path for http cache and cookie storage.
503           "/data/data/org.chromium.net/app_cronet_test/test_storage",
504           // Accept-Language request header field.
505           "foreign-language",
506           // User-Agent request header field.
507           "fake agent",
508           // JSON encoded experimental options.
509           "{\"QUIC\":{\"allow_server_migration\":true}}",
510           // MockCertVerifier to use for testing purposes.
511           std::unique_ptr<net::CertVerifier>(),
512           // Enable network quality estimator.
513           false,
514           // Enable Public Key Pinning bypass for local trust anchors.
515           true,
516           // Optional network thread priority.
517           absl::optional<double>());
518 
519   net::URLRequestContextBuilder builder;
520   config->ConfigureURLRequestContextBuilder(&builder);
521   // Set a ProxyConfigService to avoid DCHECK failure when building.
522   builder.set_proxy_config_service(
523       std::make_unique<net::ProxyConfigServiceFixed>(
524           net::ProxyConfigWithAnnotation::CreateDirect()));
525   std::unique_ptr<net::URLRequestContext> context(builder.Build());
526   const net::QuicParams* quic_params = context->quic_context()->params();
527 
528   EXPECT_FALSE(quic_params->close_sessions_on_ip_change);
529   EXPECT_TRUE(quic_params->allow_server_migration);
530 }
531 
532 // Tests that goaway_sessions_on_ip_changes can be set on via
533 // experimental options.
TEST(URLRequestContextConfigTest,SetQuicGoAwaySessionsOnIPChangeViaExperimentOptions)534 TEST(URLRequestContextConfigTest,
535      SetQuicGoAwaySessionsOnIPChangeViaExperimentOptions) {
536   base::test::TaskEnvironment task_environment_(
537       base::test::TaskEnvironment::MainThreadType::IO);
538 
539   std::unique_ptr<URLRequestContextConfig> config =
540       URLRequestContextConfig::CreateURLRequestContextConfig(
541           // Enable QUIC.
542           true,
543           // Enable SPDY.
544           true,
545           // Enable Brotli.
546           false,
547           // Type of http cache.
548           URLRequestContextConfig::HttpCacheType::DISK,
549           // Max size of http cache in bytes.
550           1024000,
551           // Disable caching for HTTP responses. Other information may be stored
552           // in the cache.
553           false,
554           // Storage path for http cache and cookie storage.
555           "/data/data/org.chromium.net/app_cronet_test/test_storage",
556           // Accept-Language request header field.
557           "foreign-language",
558           // User-Agent request header field.
559           "fake agent",
560           // JSON encoded experimental options.
561           "{\"QUIC\":{\"goaway_sessions_on_ip_change\":true}}",
562           // MockCertVerifier to use for testing purposes.
563           std::unique_ptr<net::CertVerifier>(),
564           // Enable network quality estimator.
565           false,
566           // Enable Public Key Pinning bypass for local trust anchors.
567           true,
568           // Optional network thread priority.
569           absl::optional<double>());
570 
571   net::URLRequestContextBuilder builder;
572   config->ConfigureURLRequestContextBuilder(&builder);
573   // Set a ProxyConfigService to avoid DCHECK failure when building.
574   builder.set_proxy_config_service(
575       std::make_unique<net::ProxyConfigServiceFixed>(
576           net::ProxyConfigWithAnnotation::CreateDirect()));
577   std::unique_ptr<net::URLRequestContext> context(builder.Build());
578   const net::QuicParams* quic_params = context->quic_context()->params();
579 
580   EXPECT_FALSE(quic_params->close_sessions_on_ip_change);
581   EXPECT_TRUE(quic_params->goaway_sessions_on_ip_change);
582 }
583 
TEST(URLRequestContextConfigTest,SetQuicConnectionMigrationV2Options)584 TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationV2Options) {
585   base::test::TaskEnvironment task_environment_(
586       base::test::TaskEnvironment::MainThreadType::IO);
587 
588   std::unique_ptr<URLRequestContextConfig> config =
589       URLRequestContextConfig::CreateURLRequestContextConfig(
590           // Enable QUIC.
591           true,
592           // Enable SPDY.
593           true,
594           // Enable Brotli.
595           false,
596           // Type of http cache.
597           URLRequestContextConfig::HttpCacheType::DISK,
598           // Max size of http cache in bytes.
599           1024000,
600           // Disable caching for HTTP responses. Other information may be stored
601           // in the cache.
602           false,
603           // Storage path for http cache and cookie storage.
604           "/data/data/org.chromium.net/app_cronet_test/test_storage",
605           // Accept-Language request header field.
606           "foreign-language",
607           // User-Agent request header field.
608           "fake agent",
609           // JSON encoded experimental options.
610           "{\"QUIC\":{\"migrate_sessions_on_network_change_v2\":true,"
611           "\"migrate_sessions_early_v2\":true,"
612           "\"retry_on_alternate_network_before_handshake\":true,"
613           "\"migrate_idle_sessions\":true,"
614           "\"retransmittable_on_wire_timeout_milliseconds\":1000,"
615           "\"idle_session_migration_period_seconds\":15,"
616           "\"max_time_on_non_default_network_seconds\":10,"
617           "\"max_migrations_to_non_default_network_on_write_error\":3,"
618           "\"max_migrations_to_non_default_network_on_path_degrading\":4}}",
619           // MockCertVerifier to use for testing purposes.
620           std::unique_ptr<net::CertVerifier>(),
621           // Enable network quality estimator.
622           false,
623           // Enable Public Key Pinning bypass for local trust anchors.
624           true,
625           // Optional network thread priority.
626           absl::optional<double>());
627 
628   net::URLRequestContextBuilder builder;
629   config->ConfigureURLRequestContextBuilder(&builder);
630   // Set a ProxyConfigService to avoid DCHECK failure when building.
631   builder.set_proxy_config_service(
632       std::make_unique<net::ProxyConfigServiceFixed>(
633           net::ProxyConfigWithAnnotation::CreateDirect()));
634   std::unique_ptr<net::URLRequestContext> context(builder.Build());
635   const net::QuicParams* quic_params = context->quic_context()->params();
636 
637   EXPECT_TRUE(quic_params->migrate_sessions_on_network_change_v2);
638   EXPECT_TRUE(quic_params->migrate_sessions_early_v2);
639   EXPECT_TRUE(quic_params->retry_on_alternate_network_before_handshake);
640   EXPECT_EQ(1000,
641             quic_params->retransmittable_on_wire_timeout.InMilliseconds());
642   EXPECT_TRUE(quic_params->migrate_idle_sessions);
643   EXPECT_EQ(base::Seconds(15), quic_params->idle_session_migration_period);
644   EXPECT_EQ(base::Seconds(10), quic_params->max_time_on_non_default_network);
645   EXPECT_EQ(3,
646             quic_params->max_migrations_to_non_default_network_on_write_error);
647   EXPECT_EQ(
648       4, quic_params->max_migrations_to_non_default_network_on_path_degrading);
649   EXPECT_EQ(net::DefaultSupportedQuicVersions(),
650             quic_params->supported_versions);
651 }
652 
TEST(URLRequestContextConfigTest,SetQuicAllowPortMigration)653 TEST(URLRequestContextConfigTest, SetQuicAllowPortMigration) {
654   base::test::TaskEnvironment task_environment_(
655       base::test::TaskEnvironment::MainThreadType::IO);
656   std::unique_ptr<URLRequestContextConfig> config =
657       URLRequestContextConfig::CreateURLRequestContextConfig(
658           // Enable QUIC.
659           true,
660           // Enable SPDY.
661           true,
662           // Enable Brotli.
663           false,
664           // Type of http cache.
665           URLRequestContextConfig::HttpCacheType::DISK,
666           // Max size of http cache in bytes.
667           1024000,
668           // Disable caching for HTTP responses. Other information may be stored
669           // in the cache.
670           false,
671           // Storage path for http cache and cookie storage.
672           "/data/data/org.chromium.net/app_cronet_test/test_storage",
673           // Accept-Language request header field.
674           "foreign-language",
675           // User-Agent request header field.
676           "fake agent",
677           // JSON encoded experimental options.
678           "{\"QUIC\":{\"allow_port_migration\":false}}",
679           // MockCertVerifier to use for testing purposes.
680           std::unique_ptr<net::CertVerifier>(),
681           // Enable network quality estimator.
682           false,
683           // Enable Public Key Pinning bypass for local trust anchors.
684           true,
685           // Optional network thread priority.
686           absl::optional<double>());
687 
688   net::URLRequestContextBuilder builder;
689   config->ConfigureURLRequestContextBuilder(&builder);
690   // Set a ProxyConfigService to avoid DCHECK failure when building.
691   builder.set_proxy_config_service(
692       std::make_unique<net::ProxyConfigServiceFixed>(
693           net::ProxyConfigWithAnnotation::CreateDirect()));
694   std::unique_ptr<net::URLRequestContext> context(builder.Build());
695   const net::QuicParams* quic_params = context->quic_context()->params();
696 
697   EXPECT_FALSE(quic_params->allow_port_migration);
698 }
699 
TEST(URLRequestContextConfigTest,DisableQuicRetryWithoutAltSvcOnQuicErrors)700 TEST(URLRequestContextConfigTest, DisableQuicRetryWithoutAltSvcOnQuicErrors) {
701   base::test::TaskEnvironment task_environment_(
702       base::test::TaskEnvironment::MainThreadType::IO);
703   std::unique_ptr<URLRequestContextConfig> config =
704       URLRequestContextConfig::CreateURLRequestContextConfig(
705           // Enable QUIC.
706           true,
707           // Enable SPDY.
708           true,
709           // Enable Brotli.
710           false,
711           // Type of http cache.
712           URLRequestContextConfig::HttpCacheType::DISK,
713           // Max size of http cache in bytes.
714           1024000,
715           // Disable caching for HTTP responses. Other information may be stored
716           // in the cache.
717           false,
718           // Storage path for http cache and cookie storage.
719           "/data/data/org.chromium.net/app_cronet_test/test_storage",
720           // Accept-Language request header field.
721           "foreign-language",
722           // User-Agent request header field.
723           "fake agent",
724           // JSON encoded experimental options.
725           "{\"QUIC\":{\"retry_without_alt_svc_on_quic_errors\":false}}",
726           // MockCertVerifier to use for testing purposes.
727           std::unique_ptr<net::CertVerifier>(),
728           // Enable network quality estimator.
729           false,
730           // Enable Public Key Pinning bypass for local trust anchors.
731           true,
732           // Optional network thread priority.
733           absl::optional<double>());
734 
735   net::URLRequestContextBuilder builder;
736   config->ConfigureURLRequestContextBuilder(&builder);
737   // Set a ProxyConfigService to avoid DCHECK failure when building.
738   builder.set_proxy_config_service(
739       std::make_unique<net::ProxyConfigServiceFixed>(
740           net::ProxyConfigWithAnnotation::CreateDirect()));
741   std::unique_ptr<net::URLRequestContext> context(builder.Build());
742   const net::QuicParams* quic_params = context->quic_context()->params();
743 
744   EXPECT_FALSE(quic_params->retry_without_alt_svc_on_quic_errors);
745 }
746 
TEST(URLRequestContextConfigTest,BrokenAlternativeServiceDelayParams1)747 TEST(URLRequestContextConfigTest, BrokenAlternativeServiceDelayParams1) {
748   base::test::TaskEnvironment task_environment_(
749       base::test::TaskEnvironment::MainThreadType::IO);
750   std::unique_ptr<URLRequestContextConfig> config =
751       URLRequestContextConfig::CreateURLRequestContextConfig(
752           // Enable QUIC.
753           true,
754           // Enable SPDY.
755           true,
756           // Enable Brotli.
757           false,
758           // Type of http cache.
759           URLRequestContextConfig::HttpCacheType::DISK,
760           // Max size of http cache in bytes.
761           1024000,
762           // Disable caching for HTTP responses. Other information may be stored
763           // in the cache.
764           false,
765           // Storage path for http cache and cookie storage.
766           "/data/data/org.chromium.net/app_cronet_test/test_storage",
767           // Accept-Language request header field.
768           "foreign-language",
769           // User-Agent request header field.
770           "fake agent",
771           // JSON encoded experimental options.
772           "{\"QUIC\":{\"initial_delay_for_broken_alternative_service_seconds\":"
773           "1,"
774           "\"exponential_backoff_on_initial_delay\":true}}",
775           // MockCertVerifier to use for testing purposes.
776           std::unique_ptr<net::CertVerifier>(),
777           // Enable network quality estimator.
778           false,
779           // Enable Public Key Pinning bypass for local trust anchors.
780           true,
781           // Optional network thread priority.
782           absl::optional<double>());
783 
784   net::URLRequestContextBuilder builder;
785   config->ConfigureURLRequestContextBuilder(&builder);
786   // Set a ProxyConfigService to avoid DCHECK failure when building.
787   builder.set_proxy_config_service(
788       std::make_unique<net::ProxyConfigServiceFixed>(
789           net::ProxyConfigWithAnnotation::CreateDirect()));
790   std::unique_ptr<net::URLRequestContext> context(builder.Build());
791   const net::QuicParams* quic_params = context->quic_context()->params();
792 
793   ASSERT_TRUE(
794       quic_params->initial_delay_for_broken_alternative_service.has_value());
795   EXPECT_EQ(base::Seconds(1),
796             quic_params->initial_delay_for_broken_alternative_service.value());
797   ASSERT_TRUE(quic_params->exponential_backoff_on_initial_delay.has_value());
798   EXPECT_TRUE(quic_params->exponential_backoff_on_initial_delay.value());
799 }
800 
TEST(URLRequestContextConfigTest,BrokenAlternativeServiceDelayParams2)801 TEST(URLRequestContextConfigTest, BrokenAlternativeServiceDelayParams2) {
802   base::test::TaskEnvironment task_environment_(
803       base::test::TaskEnvironment::MainThreadType::IO);
804   std::unique_ptr<URLRequestContextConfig> config =
805       URLRequestContextConfig::CreateURLRequestContextConfig(
806           // Enable QUIC.
807           true,
808           // Enable SPDY.
809           true,
810           // Enable Brotli.
811           false,
812           // Type of http cache.
813           URLRequestContextConfig::HttpCacheType::DISK,
814           // Max size of http cache in bytes.
815           1024000,
816           // Disable caching for HTTP responses. Other information may be stored
817           // in the cache.
818           false,
819           // Storage path for http cache and cookie storage.
820           "/data/data/org.chromium.net/app_cronet_test/test_storage",
821           // Accept-Language request header field.
822           "foreign-language",
823           // User-Agent request header field.
824           "fake agent",
825           // JSON encoded experimental options.
826           "{\"QUIC\":{\"initial_delay_for_broken_alternative_service_seconds\":"
827           "5,"
828           "\"exponential_backoff_on_initial_delay\":false}}",
829           // MockCertVerifier to use for testing purposes.
830           std::unique_ptr<net::CertVerifier>(),
831           // Enable network quality estimator.
832           false,
833           // Enable Public Key Pinning bypass for local trust anchors.
834           true,
835           // Optional network thread priority.
836           absl::optional<double>());
837 
838   net::URLRequestContextBuilder builder;
839   config->ConfigureURLRequestContextBuilder(&builder);
840   // Set a ProxyConfigService to avoid DCHECK failure when building.
841   builder.set_proxy_config_service(
842       std::make_unique<net::ProxyConfigServiceFixed>(
843           net::ProxyConfigWithAnnotation::CreateDirect()));
844   std::unique_ptr<net::URLRequestContext> context(builder.Build());
845   const net::QuicParams* quic_params = context->quic_context()->params();
846 
847   ASSERT_TRUE(
848       quic_params->initial_delay_for_broken_alternative_service.has_value());
849   EXPECT_EQ(base::Seconds(5),
850             quic_params->initial_delay_for_broken_alternative_service.value());
851   ASSERT_TRUE(quic_params->exponential_backoff_on_initial_delay.has_value());
852   EXPECT_FALSE(quic_params->exponential_backoff_on_initial_delay.value());
853 }
854 
TEST(URLRequestContextConfigTest,DelayMainJobWithAvailableSpdySession)855 TEST(URLRequestContextConfigTest, DelayMainJobWithAvailableSpdySession) {
856   base::test::TaskEnvironment task_environment_(
857       base::test::TaskEnvironment::MainThreadType::IO);
858   std::unique_ptr<URLRequestContextConfig> config =
859       URLRequestContextConfig::CreateURLRequestContextConfig(
860           // Enable QUIC.
861           true,
862           // Enable SPDY.
863           true,
864           // Enable Brotli.
865           false,
866           // Type of http cache.
867           URLRequestContextConfig::HttpCacheType::DISK,
868           // Max size of http cache in bytes.
869           1024000,
870           // Disable caching for HTTP responses. Other information may be stored
871           // in the cache.
872           false,
873           // Storage path for http cache and cookie storage.
874           "/data/data/org.chromium.net/app_cronet_test/test_storage",
875           // Accept-Language request header field.
876           "foreign-language",
877           // User-Agent request header field.
878           "fake agent",
879           // JSON encoded experimental options.
880           "{\"QUIC\":{\"delay_main_job_with_available_spdy_session\":true}}",
881           // MockCertVerifier to use for testing purposes.
882           std::unique_ptr<net::CertVerifier>(),
883           // Enable network quality estimator.
884           false,
885           // Enable Public Key Pinning bypass for local trust anchors.
886           true,
887           // Optional network thread priority.
888           absl::optional<double>());
889 
890   net::URLRequestContextBuilder builder;
891   config->ConfigureURLRequestContextBuilder(&builder);
892   // Set a ProxyConfigService to avoid DCHECK failure when building.
893   builder.set_proxy_config_service(
894       std::make_unique<net::ProxyConfigServiceFixed>(
895           net::ProxyConfigWithAnnotation::CreateDirect()));
896   std::unique_ptr<net::URLRequestContext> context(builder.Build());
897   const net::QuicParams* quic_params = context->quic_context()->params();
898 
899   EXPECT_TRUE(quic_params->delay_main_job_with_available_spdy_session);
900 }
901 
TEST(URLRequestContextConfigTest,SetDisableTlsZeroRtt)902 TEST(URLRequestContextConfigTest, SetDisableTlsZeroRtt) {
903   base::test::TaskEnvironment task_environment_(
904       base::test::TaskEnvironment::MainThreadType::IO);
905   std::unique_ptr<URLRequestContextConfig> config =
906       URLRequestContextConfig::CreateURLRequestContextConfig(
907           // Enable QUIC.
908           true,
909           // Enable SPDY.
910           true,
911           // Enable Brotli.
912           false,
913           // Type of http cache.
914           URLRequestContextConfig::HttpCacheType::DISK,
915           // Max size of http cache in bytes.
916           1024000,
917           // Disable caching for HTTP responses. Other information may be stored
918           // in the cache.
919           false,
920           // Storage path for http cache and cookie storage.
921           "/data/data/org.chromium.net/app_cronet_test/test_storage",
922           // Accept-Language request header field.
923           "foreign-language",
924           // User-Agent request header field.
925           "fake agent",
926           // JSON encoded experimental options.
927           "{\"QUIC\":{\"disable_tls_zero_rtt\":true}}",
928           // MockCertVerifier to use for testing purposes.
929           std::unique_ptr<net::CertVerifier>(),
930           // Enable network quality estimator.
931           false,
932           // Enable Public Key Pinning bypass for local trust anchors.
933           true,
934           // Optional network thread priority.
935           absl::optional<double>());
936 
937   net::URLRequestContextBuilder builder;
938   config->ConfigureURLRequestContextBuilder(&builder);
939   // Set a ProxyConfigService to avoid DCHECK failure when building.
940   builder.set_proxy_config_service(
941       std::make_unique<net::ProxyConfigServiceFixed>(
942           net::ProxyConfigWithAnnotation::CreateDirect()));
943   std::unique_ptr<net::URLRequestContext> context(builder.Build());
944   const net::QuicParams* quic_params = context->quic_context()->params();
945 
946   EXPECT_TRUE(quic_params->disable_tls_zero_rtt);
947 }
948 
TEST(URLRequestContextConfigTest,SetQuicHostWhitelist)949 TEST(URLRequestContextConfigTest, SetQuicHostWhitelist) {
950   base::test::TaskEnvironment task_environment_(
951       base::test::TaskEnvironment::MainThreadType::IO);
952 
953   std::unique_ptr<URLRequestContextConfig> config =
954       URLRequestContextConfig::CreateURLRequestContextConfig(
955           // Enable QUIC.
956           true,
957           // Enable SPDY.
958           true,
959           // Enable Brotli.
960           false,
961           // Type of http cache.
962           URLRequestContextConfig::HttpCacheType::DISK,
963           // Max size of http cache in bytes.
964           1024000,
965           // Disable caching for HTTP responses. Other information may be stored
966           // in the cache.
967           false,
968           // Storage path for http cache and cookie storage.
969           "/data/data/org.chromium.net/app_cronet_test/test_storage",
970           // Accept-Language request header field.
971           "foreign-language",
972           // User-Agent request header field.
973           "fake agent",
974           // JSON encoded experimental options.
975           "{\"QUIC\":{\"host_whitelist\":\"www.example.com,www.example.org\"}}",
976           // MockCertVerifier to use for testing purposes.
977           std::unique_ptr<net::CertVerifier>(),
978           // Enable network quality estimator.
979           false,
980           // Enable Public Key Pinning bypass for local trust anchors.
981           true,
982           // Optional network thread priority.
983           absl::optional<double>());
984 
985   net::URLRequestContextBuilder builder;
986   config->ConfigureURLRequestContextBuilder(&builder);
987   // Set a ProxyConfigService to avoid DCHECK failure when building.
988   builder.set_proxy_config_service(
989       std::make_unique<net::ProxyConfigServiceFixed>(
990           net::ProxyConfigWithAnnotation::CreateDirect()));
991   std::unique_ptr<net::URLRequestContext> context(builder.Build());
992   const net::HttpNetworkSessionParams* params =
993       context->GetNetworkSessionParams();
994 
995   EXPECT_TRUE(base::Contains(params->quic_host_allowlist, "www.example.com"));
996   EXPECT_TRUE(base::Contains(params->quic_host_allowlist, "www.example.org"));
997 }
998 
TEST(URLRequestContextConfigTest,SetQuicMaxTimeBeforeCryptoHandshake)999 TEST(URLRequestContextConfigTest, SetQuicMaxTimeBeforeCryptoHandshake) {
1000   base::test::TaskEnvironment task_environment_(
1001       base::test::TaskEnvironment::MainThreadType::IO);
1002 
1003   std::unique_ptr<URLRequestContextConfig> config =
1004       URLRequestContextConfig::CreateURLRequestContextConfig(
1005           // Enable QUIC.
1006           true,
1007           // Enable SPDY.
1008           true,
1009           // Enable Brotli.
1010           false,
1011           // Type of http cache.
1012           URLRequestContextConfig::HttpCacheType::DISK,
1013           // Max size of http cache in bytes.
1014           1024000,
1015           // Disable caching for HTTP responses. Other information may be stored
1016           // in the cache.
1017           false,
1018           // Storage path for http cache and cookie storage.
1019           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1020           // Accept-Language request header field.
1021           "foreign-language",
1022           // User-Agent request header field.
1023           "fake agent",
1024           // JSON encoded experimental options.
1025           "{\"QUIC\":{\"max_time_before_crypto_handshake_seconds\":7,"
1026           "\"max_idle_time_before_crypto_handshake_seconds\":11}}",
1027           // MockCertVerifier to use for testing purposes.
1028           std::unique_ptr<net::CertVerifier>(),
1029           // Enable network quality estimator.
1030           false,
1031           // Enable Public Key Pinning bypass for local trust anchors.
1032           true,
1033           // Optional network thread priority.
1034           absl::optional<double>());
1035 
1036   net::URLRequestContextBuilder builder;
1037   config->ConfigureURLRequestContextBuilder(&builder);
1038   // Set a ProxyConfigService to avoid DCHECK failure when building.
1039   builder.set_proxy_config_service(
1040       std::make_unique<net::ProxyConfigServiceFixed>(
1041           net::ProxyConfigWithAnnotation::CreateDirect()));
1042   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1043   const net::QuicParams* quic_params = context->quic_context()->params();
1044 
1045   EXPECT_EQ(7, quic_params->max_time_before_crypto_handshake.InSeconds());
1046   EXPECT_EQ(11, quic_params->max_idle_time_before_crypto_handshake.InSeconds());
1047 }
1048 
TEST(URLURLRequestContextConfigTest,SetQuicConnectionOptions)1049 TEST(URLURLRequestContextConfigTest, SetQuicConnectionOptions) {
1050   base::test::TaskEnvironment task_environment_(
1051       base::test::TaskEnvironment::MainThreadType::IO);
1052 
1053   std::unique_ptr<URLRequestContextConfig> config =
1054       URLRequestContextConfig::CreateURLRequestContextConfig(
1055           // Enable QUIC.
1056           true,
1057           // Enable SPDY.
1058           true,
1059           // Enable Brotli.
1060           false,
1061           // Type of http cache.
1062           URLRequestContextConfig::HttpCacheType::DISK,
1063           // Max size of http cache in bytes.
1064           1024000,
1065           // Disable caching for HTTP responses. Other information may be stored
1066           // in the cache.
1067           false,
1068           // Storage path for http cache and cookie storage.
1069           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1070           // Accept-Language request header field.
1071           "foreign-language",
1072           // User-Agent request header field.
1073           "fake agent",
1074           // JSON encoded experimental options.
1075           "{\"QUIC\":{\"connection_options\":\"TIME,TBBR,REJ\","
1076           "\"client_connection_options\":\"TBBR,1RTT\"}}",
1077           // MockCertVerifier to use for testing purposes.
1078           std::unique_ptr<net::CertVerifier>(),
1079           // Enable network quality estimator.
1080           false,
1081           // Enable Public Key Pinning bypass for local trust anchors.
1082           true,
1083           // Optional network thread priority.
1084           absl::optional<double>());
1085 
1086   net::URLRequestContextBuilder builder;
1087   config->ConfigureURLRequestContextBuilder(&builder);
1088   // Set a ProxyConfigService to avoid DCHECK failure when building.
1089   builder.set_proxy_config_service(
1090       std::make_unique<net::ProxyConfigServiceFixed>(
1091           net::ProxyConfigWithAnnotation::CreateDirect()));
1092   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1093   const net::QuicParams* quic_params = context->quic_context()->params();
1094 
1095   quic::QuicTagVector connection_options;
1096   connection_options.push_back(quic::kTIME);
1097   connection_options.push_back(quic::kTBBR);
1098   connection_options.push_back(quic::kREJ);
1099   EXPECT_EQ(connection_options, quic_params->connection_options);
1100 
1101   quic::QuicTagVector client_connection_options;
1102   client_connection_options.push_back(quic::kTBBR);
1103   client_connection_options.push_back(quic::k1RTT);
1104   EXPECT_EQ(client_connection_options, quic_params->client_connection_options);
1105 }
1106 
TEST(URLURLRequestContextConfigTest,SetAcceptLanguageAndUserAgent)1107 TEST(URLURLRequestContextConfigTest, SetAcceptLanguageAndUserAgent) {
1108   base::test::TaskEnvironment task_environment_(
1109       base::test::TaskEnvironment::MainThreadType::IO);
1110 
1111   std::unique_ptr<URLRequestContextConfig> config =
1112       URLRequestContextConfig::CreateURLRequestContextConfig(
1113           // Enable QUIC.
1114           true,
1115           // Enable SPDY.
1116           true,
1117           // Enable Brotli.
1118           false,
1119           // Type of http cache.
1120           URLRequestContextConfig::HttpCacheType::DISK,
1121           // Max size of http cache in bytes.
1122           1024000,
1123           // Disable caching for HTTP responses. Other information may be stored
1124           // in the cache.
1125           false,
1126           // Storage path for http cache and cookie storage.
1127           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1128           // Accept-Language request header field.
1129           "foreign-language",
1130           // User-Agent request header field.
1131           "fake agent",
1132           // JSON encoded experimental options.
1133           "{}",
1134           // MockCertVerifier to use for testing purposes.
1135           std::unique_ptr<net::CertVerifier>(),
1136           // Enable network quality estimator.
1137           false,
1138           // Enable Public Key Pinning bypass for local trust anchors.
1139           true,
1140           // Optional network thread priority.
1141           absl::optional<double>());
1142 
1143   net::URLRequestContextBuilder builder;
1144   config->ConfigureURLRequestContextBuilder(&builder);
1145   // Set a ProxyConfigService to avoid DCHECK failure when building.
1146   builder.set_proxy_config_service(
1147       std::make_unique<net::ProxyConfigServiceFixed>(
1148           net::ProxyConfigWithAnnotation::CreateDirect()));
1149   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1150   EXPECT_EQ("foreign-language",
1151             context->http_user_agent_settings()->GetAcceptLanguage());
1152   EXPECT_EQ("fake agent", context->http_user_agent_settings()->GetUserAgent());
1153 }
1154 
TEST(URLURLRequestContextConfigTest,TurningOffQuic)1155 TEST(URLURLRequestContextConfigTest, TurningOffQuic) {
1156   base::test::TaskEnvironment task_environment_(
1157       base::test::TaskEnvironment::MainThreadType::IO);
1158 
1159   std::unique_ptr<URLRequestContextConfig> config =
1160       URLRequestContextConfig::CreateURLRequestContextConfig(
1161           // Enable QUIC.
1162           false,
1163           // Enable SPDY.
1164           true,
1165           // Enable Brotli.
1166           false,
1167           // Type of http cache.
1168           URLRequestContextConfig::HttpCacheType::DISK,
1169           // Max size of http cache in bytes.
1170           1024000,
1171           // Disable caching for HTTP responses. Other information may be stored
1172           // in the cache.
1173           false,
1174           // Storage path for http cache and cookie storage.
1175           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1176           // Accept-Language request header field.
1177           "foreign-language",
1178           // User-Agent request header field.
1179           "fake agent",
1180           // JSON encoded experimental options.
1181           "{}",
1182           // MockCertVerifier to use for testing purposes.
1183           std::unique_ptr<net::CertVerifier>(),
1184           // Enable network quality estimator.
1185           false,
1186           // Enable Public Key Pinning bypass for local trust anchors.
1187           true,
1188           // Optional network thread priority.
1189           absl::optional<double>());
1190 
1191   net::URLRequestContextBuilder builder;
1192   config->ConfigureURLRequestContextBuilder(&builder);
1193   // Set a ProxyConfigService to avoid DCHECK failure when building.
1194   builder.set_proxy_config_service(
1195       std::make_unique<net::ProxyConfigServiceFixed>(
1196           net::ProxyConfigWithAnnotation::CreateDirect()));
1197   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1198   const net::HttpNetworkSessionParams* params =
1199       context->GetNetworkSessionParams();
1200   EXPECT_EQ(false, params->enable_quic);
1201 }
1202 
TEST(URLURLRequestContextConfigTest,DefaultEnableQuic)1203 TEST(URLURLRequestContextConfigTest, DefaultEnableQuic) {
1204   base::test::TaskEnvironment task_environment_(
1205       base::test::TaskEnvironment::MainThreadType::IO);
1206 
1207   URLRequestContextConfigBuilder config_builder;
1208   std::unique_ptr<URLRequestContextConfig> config = config_builder.Build();
1209   net::URLRequestContextBuilder builder;
1210   config->ConfigureURLRequestContextBuilder(&builder);
1211   // Set a ProxyConfigService to avoid DCHECK failure when building.
1212   builder.set_proxy_config_service(
1213       std::make_unique<net::ProxyConfigServiceFixed>(
1214           net::ProxyConfigWithAnnotation::CreateDirect()));
1215   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1216   const net::HttpNetworkSessionParams* params =
1217       context->GetNetworkSessionParams();
1218   EXPECT_EQ(true, params->enable_quic);
1219 }
1220 
TEST(URLRequestContextConfigTest,SetSpdyGoAwayOnIPChange)1221 TEST(URLRequestContextConfigTest, SetSpdyGoAwayOnIPChange) {
1222   base::test::TaskEnvironment task_environment_(
1223       base::test::TaskEnvironment::MainThreadType::IO);
1224 
1225   std::unique_ptr<URLRequestContextConfig> config =
1226       URLRequestContextConfig::CreateURLRequestContextConfig(
1227           // Enable QUIC.
1228           true,
1229           // Enable SPDY.
1230           true,
1231           // Enable Brotli.
1232           false,
1233           // Type of http cache.
1234           URLRequestContextConfig::HttpCacheType::DISK,
1235           // Max size of http cache in bytes.
1236           1024000,
1237           // Disable caching for HTTP responses. Other information may be stored
1238           // in the cache.
1239           false,
1240           // Storage path for http cache and cookie storage.
1241           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1242           // Accept-Language request header field.
1243           "foreign-language",
1244           // User-Agent request header field.
1245           "fake agent",
1246           // JSON encoded experimental options.
1247           "{\"spdy_go_away_on_ip_change\":false}",
1248           // MockCertVerifier to use for testing purposes.
1249           std::unique_ptr<net::CertVerifier>(),
1250           // Enable network quality estimator.
1251           false,
1252           // Enable Public Key Pinning bypass for local trust anchors.
1253           true,
1254           // Optional network thread priority.
1255           absl::optional<double>());
1256 
1257   net::URLRequestContextBuilder builder;
1258   config->ConfigureURLRequestContextBuilder(&builder);
1259   // Set a ProxyConfigService to avoid DCHECK failure when building.
1260   builder.set_proxy_config_service(
1261       std::make_unique<net::ProxyConfigServiceFixed>(
1262           net::ProxyConfigWithAnnotation::CreateDirect()));
1263   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1264   const net::HttpNetworkSessionParams* params =
1265       context->GetNetworkSessionParams();
1266   EXPECT_FALSE(params->spdy_go_away_on_ip_change);
1267 }
1268 
TEST(URLRequestContextConfigTest,WrongSpdyGoAwayOnIPChangeValue)1269 TEST(URLRequestContextConfigTest, WrongSpdyGoAwayOnIPChangeValue) {
1270   base::test::TaskEnvironment task_environment_(
1271       base::test::TaskEnvironment::MainThreadType::IO);
1272 
1273   std::unique_ptr<URLRequestContextConfig> config =
1274       URLRequestContextConfig::CreateURLRequestContextConfig(
1275           // Enable QUIC.
1276           true,
1277           // Enable SPDY.
1278           true,
1279           // Enable Brotli.
1280           false,
1281           // Type of http cache.
1282           URLRequestContextConfig::HttpCacheType::DISK,
1283           // Max size of http cache in bytes.
1284           1024000,
1285           // Disable caching for HTTP responses. Other information may be stored
1286           // in the cache.
1287           false,
1288           // Storage path for http cache and cookie storage.
1289           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1290           // Accept-Language request header field.
1291           "foreign-language",
1292           // User-Agent request header field.
1293           "fake agent",
1294           // JSON encoded experimental options.
1295           "{\"spdy_go_away_on_ip_change\":\"not a bool\"}",
1296           // MockCertVerifier to use for testing purposes.
1297           std::unique_ptr<net::CertVerifier>(),
1298           // Enable network quality estimator.
1299           false,
1300           // Enable Public Key Pinning bypass for local trust anchors.
1301           true,
1302           // Optional network thread priority.
1303           absl::optional<double>());
1304 
1305   net::URLRequestContextBuilder builder;
1306   config->ConfigureURLRequestContextBuilder(&builder);
1307   EXPECT_FALSE(config->effective_experimental_options.contains(
1308       "spdy_go_away_on_ip_change"));
1309 }
1310 
TEST(URLRequestContextConfigTest,BidiStreamDetectBrokenConnection)1311 TEST(URLRequestContextConfigTest, BidiStreamDetectBrokenConnection) {
1312   base::test::TaskEnvironment task_environment_(
1313       base::test::TaskEnvironment::MainThreadType::IO);
1314 
1315   std::unique_ptr<URLRequestContextConfig> config =
1316       URLRequestContextConfig::CreateURLRequestContextConfig(
1317           // Enable QUIC.
1318           true,
1319           // Enable SPDY.
1320           true,
1321           // Enable Brotli.
1322           false,
1323           // Type of http cache.
1324           URLRequestContextConfig::HttpCacheType::DISK,
1325           // Max size of http cache in bytes.
1326           1024000,
1327           // Disable caching for HTTP responses. Other information may be stored
1328           // in the cache.
1329           false,
1330           // Storage path for http cache and cookie storage.
1331           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1332           // Accept-Language request header field.
1333           "foreign-language",
1334           // User-Agent request header field.
1335           "fake agent",
1336           // JSON encoded experimental options.
1337           "{\"bidi_stream_detect_broken_connection\":10}",
1338           // MockCertVerifier to use for testing purposes.
1339           std::unique_ptr<net::CertVerifier>(),
1340           // Enable network quality estimator.
1341           false,
1342           // Enable Public Key Pinning bypass for local trust anchors.
1343           true,
1344           // Optional network thread priority.
1345           absl::optional<double>());
1346 
1347   net::URLRequestContextBuilder builder;
1348   config->ConfigureURLRequestContextBuilder(&builder);
1349   EXPECT_TRUE(config->effective_experimental_options.contains(
1350       "bidi_stream_detect_broken_connection"));
1351   EXPECT_TRUE(config->bidi_stream_detect_broken_connection);
1352   EXPECT_EQ(config->heartbeat_interval, base::Seconds(10));
1353 }
1354 
TEST(URLRequestContextConfigTest,WrongBidiStreamDetectBrokenConnectionValue)1355 TEST(URLRequestContextConfigTest, WrongBidiStreamDetectBrokenConnectionValue) {
1356   base::test::TaskEnvironment task_environment_(
1357       base::test::TaskEnvironment::MainThreadType::IO);
1358 
1359   std::unique_ptr<URLRequestContextConfig> config =
1360       URLRequestContextConfig::CreateURLRequestContextConfig(
1361           // Enable QUIC.
1362           true,
1363           // Enable SPDY.
1364           true,
1365           // Enable Brotli.
1366           false,
1367           // Type of http cache.
1368           URLRequestContextConfig::HttpCacheType::DISK,
1369           // Max size of http cache in bytes.
1370           1024000,
1371           // Disable caching for HTTP responses. Other information may be stored
1372           // in the cache.
1373           false,
1374           // Storage path for http cache and cookie storage.
1375           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1376           // Accept-Language request header field.
1377           "foreign-language",
1378           // User-Agent request header field.
1379           "fake agent",
1380           // JSON encoded experimental options.
1381           "{\"bidi_stream_detect_broken_connection\": \"not an int\"}",
1382           // MockCertVerifier to use for testing purposes.
1383           std::unique_ptr<net::CertVerifier>(),
1384           // Enable network quality estimator.
1385           false,
1386           // Enable Public Key Pinning bypass for local trust anchors.
1387           true,
1388           // Optional network thread priority.
1389           absl::optional<double>());
1390 
1391   net::URLRequestContextBuilder builder;
1392   config->ConfigureURLRequestContextBuilder(&builder);
1393   EXPECT_FALSE(config->effective_experimental_options.contains(
1394       "bidi_stream_detect_broken_connection"));
1395 }
1396 
TEST(URLRequestContextConfigTest,HttpsSvcbOptions)1397 TEST(URLRequestContextConfigTest, HttpsSvcbOptions) {
1398   base::test::TaskEnvironment task_environment_(
1399       base::test::TaskEnvironment::MainThreadType::IO);
1400 
1401   std::unique_ptr<URLRequestContextConfig> config =
1402       URLRequestContextConfig::CreateURLRequestContextConfig(
1403           // Enable QUIC.
1404           true,
1405           // Enable SPDY.
1406           true,
1407           // Enable Brotli.
1408           false,
1409           // Type of http cache.
1410           URLRequestContextConfig::HttpCacheType::DISK,
1411           // Max size of http cache in bytes.
1412           1024000,
1413           // Disable caching for HTTP responses. Other information may be stored
1414           // in the cache.
1415           false,
1416           // Storage path for http cache and cookie storage.
1417           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1418           // Accept-Language request header field.
1419           "foreign-language",
1420           // User-Agent request header field.
1421           "fake agent",
1422           // JSON encoded experimental options.
1423           "{\"UseDnsHttpsSvcb\":{\"enable\":true,"
1424           "\"insecure_extra_time_max\":\"1ms\","
1425           "\"insecure_extra_time_percent\":2,"
1426           "\"insecure_extra_time_min\":\"3ms\","
1427           "\"secure_extra_time_max\":\"4ms\","
1428           "\"secure_extra_time_percent\":5,"
1429           "\"secure_extra_time_min\":\"6ms\","
1430           "\"use_alpn\":true"
1431           "}}",
1432           // MockCertVerifier to use for testing purposes.
1433           std::unique_ptr<net::CertVerifier>(),
1434           // Enable network quality estimator.
1435           false,
1436           // Enable Public Key Pinning bypass for local trust anchors.
1437           true,
1438           // Optional network thread priority.
1439           absl::optional<double>());
1440   net::URLRequestContextBuilder builder;
1441   config->ConfigureURLRequestContextBuilder(&builder);
1442   // Set a ProxyConfigService to avoid DCHECK failure when building.
1443   builder.set_proxy_config_service(
1444       std::make_unique<net::ProxyConfigServiceFixed>(
1445           net::ProxyConfigWithAnnotation::CreateDirect()));
1446   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1447 
1448   const net::HostResolver::HttpsSvcbOptions& https_svcb_options =
1449       context->host_resolver()
1450           ->GetManagerForTesting()
1451           ->https_svcb_options_for_testing();
1452   EXPECT_TRUE(https_svcb_options.enable);
1453   EXPECT_EQ(base::Milliseconds(1), https_svcb_options.insecure_extra_time_max);
1454   EXPECT_EQ(2, https_svcb_options.insecure_extra_time_percent);
1455   EXPECT_EQ(base::Milliseconds(3), https_svcb_options.insecure_extra_time_min);
1456   EXPECT_EQ(base::Milliseconds(4), https_svcb_options.secure_extra_time_max);
1457   EXPECT_EQ(5, https_svcb_options.secure_extra_time_percent);
1458   EXPECT_EQ(base::Milliseconds(6), https_svcb_options.secure_extra_time_min);
1459 
1460   const net::HttpNetworkSessionParams* params =
1461       context->GetNetworkSessionParams();
1462   EXPECT_TRUE(params->use_dns_https_svcb_alpn);
1463 }
1464 
TEST(URLRequestContextConfigTest,DisableTelemetry)1465 TEST(URLRequestContextConfigTest, DisableTelemetry) {
1466   base::test::TaskEnvironment task_environment_(
1467       base::test::TaskEnvironment::MainThreadType::IO);
1468 
1469   std::unique_ptr<URLRequestContextConfig> config =
1470       URLRequestContextConfig::CreateURLRequestContextConfig(
1471           // Enable QUIC.
1472           true,
1473           // Enable SPDY.
1474           true,
1475           // Enable Brotli.
1476           false,
1477           // Type of http cache.
1478           URLRequestContextConfig::HttpCacheType::DISK,
1479           // Max size of http cache in bytes.
1480           1024000,
1481           // Disable caching for HTTP responses. Other information may be stored
1482           // in the cache.
1483           false,
1484           // Storage path for http cache and cookie storage.
1485           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1486           // Accept-Language request header field.
1487           "foreign-language",
1488           // User-Agent request header field.
1489           "fake agent",
1490           // JSON encoded experimental options.
1491           "{\"enable_telemetry\":false}",
1492           // MockCertVerifier to use for testing purposes.
1493           std::unique_ptr<net::CertVerifier>(),
1494           // Enable network quality estimator.
1495           false,
1496           // Enable Public Key Pinning bypass for local trust anchors.
1497           true,
1498           // Optional network thread priority.
1499           absl::optional<double>());
1500   net::URLRequestContextBuilder builder;
1501   config->ConfigureURLRequestContextBuilder(&builder);
1502   EXPECT_TRUE(
1503       config->effective_experimental_options.contains("enable_telemetry"));
1504   EXPECT_FALSE(config->enable_telemetry);
1505 }
1506 
TEST(URLRequestContextConfigTest,WrongEnableTelemetryValue)1507 TEST(URLRequestContextConfigTest, WrongEnableTelemetryValue) {
1508   base::test::TaskEnvironment task_environment_(
1509       base::test::TaskEnvironment::MainThreadType::IO);
1510 
1511   std::unique_ptr<URLRequestContextConfig> config =
1512       URLRequestContextConfig::CreateURLRequestContextConfig(
1513           // Enable QUIC.
1514           true,
1515           // Enable SPDY.
1516           true,
1517           // Enable Brotli.
1518           false,
1519           // Type of http cache.
1520           URLRequestContextConfig::HttpCacheType::DISK,
1521           // Max size of http cache in bytes.
1522           1024000,
1523           // Disable caching for HTTP responses. Other information may be stored
1524           // in the cache.
1525           false,
1526           // Storage path for http cache and cookie storage.
1527           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1528           // Accept-Language request header field.
1529           "foreign-language",
1530           // User-Agent request header field.
1531           "fake agent",
1532           // JSON encoded experimental options.
1533           "{\"enable_telemetry\":10}",
1534           // MockCertVerifier to use for testing purposes.
1535           std::unique_ptr<net::CertVerifier>(),
1536           // Enable network quality estimator.
1537           false,
1538           // Enable Public Key Pinning bypass for local trust anchors.
1539           true,
1540           // Optional network thread priority.
1541           absl::optional<double>());
1542 
1543   net::URLRequestContextBuilder builder;
1544   config->ConfigureURLRequestContextBuilder(&builder);
1545   EXPECT_FALSE(
1546       config->effective_experimental_options.contains("enable_telemetry"));
1547   EXPECT_TRUE(config->enable_telemetry);
1548 }
1549 
1550 // See stale_host_resolver_unittest.cc for test of StaleDNS options.
1551 
1552 }  // namespace cronet
1553