xref: /aosp_15_r20/external/cronet/components/cronet/url_request_context_config_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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           std::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(), std::nullopt);
296   EXPECT_EQ(net::OK, resolve_request->Start(
__anon5f3c32770202(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 
327 TEST(URLRequestContextConfigTest, SetSupportedQuicVersionByAlpn) {
328   base::test::TaskEnvironment task_environment_(
329       base::test::TaskEnvironment::MainThreadType::IO);
330 
331   quic::ParsedQuicVersion version = quic::AllSupportedVersions().front();
332   std::string experimental_options = "{\"QUIC\":{\"quic_version\":\"" +
333                                      quic::ParsedQuicVersionToString(version) +
334                                      "\"}}";
335 
336   std::unique_ptr<URLRequestContextConfig> config =
337       URLRequestContextConfig::CreateURLRequestContextConfig(
338           // Enable QUIC.
339           true,
340           // Enable SPDY.
341           true,
342           // Enable Brotli.
343           false,
344           // Type of http cache.
345           URLRequestContextConfig::HttpCacheType::DISK,
346           // Max size of http cache in bytes.
347           1024000,
348           // Disable caching for HTTP responses. Other information may be stored
349           // in the cache.
350           false,
351           // Storage path for http cache and cookie storage.
352           "/data/data/org.chromium.net/app_cronet_test/test_storage",
353           // Accept-Language request header field.
354           "foreign-language",
355           // User-Agent request header field.
356           "fake agent",
357           // JSON encoded experimental options.
358           experimental_options,
359           // MockCertVerifier to use for testing purposes.
360           std::unique_ptr<net::CertVerifier>(),
361           // Enable network quality estimator.
362           false,
363           // Enable Public Key Pinning bypass for local trust anchors.
364           true,
365           // Optional network thread priority.
366           std::nullopt);
367 
368   net::URLRequestContextBuilder builder;
369   config->ConfigureURLRequestContextBuilder(&builder);
370   // Set a ProxyConfigService to avoid DCHECK failure when building.
371   builder.set_proxy_config_service(
372       std::make_unique<net::ProxyConfigServiceFixed>(
373           net::ProxyConfigWithAnnotation::CreateDirect()));
374   std::unique_ptr<net::URLRequestContext> context(builder.Build());
375   const net::QuicParams* quic_params = context->quic_context()->params();
376   EXPECT_EQ(quic_params->supported_versions.size(), 1u);
377   EXPECT_EQ(quic_params->supported_versions[0], version);
378 }
379 
TEST(URLRequestContextConfigTest,SetUnsupportedQuicVersion)380 TEST(URLRequestContextConfigTest, SetUnsupportedQuicVersion) {
381   base::test::TaskEnvironment task_environment_(
382       base::test::TaskEnvironment::MainThreadType::IO);
383 
384   std::unique_ptr<URLRequestContextConfig> config =
385       URLRequestContextConfig::CreateURLRequestContextConfig(
386           // Enable QUIC.
387           true,
388           // Enable SPDY.
389           true,
390           // Enable Brotli.
391           false,
392           // Type of http cache.
393           URLRequestContextConfig::HttpCacheType::DISK,
394           // Max size of http cache in bytes.
395           1024000,
396           // Disable caching for HTTP responses. Other information may be stored
397           // in the cache.
398           false,
399           // Storage path for http cache and cookie storage.
400           "/data/data/org.chromium.net/app_cronet_test/test_storage",
401           // Accept-Language request header field.
402           "foreign-language",
403           // User-Agent request header field.
404           "fake agent",
405           // JSON encoded experimental options.
406           "{\"QUIC\":{\"quic_version\":\"h3-Q047\"}}",
407           // MockCertVerifier to use for testing purposes.
408           std::unique_ptr<net::CertVerifier>(),
409           // Enable network quality estimator.
410           false,
411           // Enable Public Key Pinning bypass for local trust anchors.
412           true,
413           // Optional network thread priority.
414           std::nullopt);
415 
416   net::URLRequestContextBuilder builder;
417   config->ConfigureURLRequestContextBuilder(&builder);
418   // Set a ProxyConfigService to avoid DCHECK failure when building.
419   builder.set_proxy_config_service(
420       std::make_unique<net::ProxyConfigServiceFixed>(
421           net::ProxyConfigWithAnnotation::CreateDirect()));
422   std::unique_ptr<net::URLRequestContext> context(builder.Build());
423   const net::QuicParams* quic_params = context->quic_context()->params();
424   EXPECT_EQ(quic_params->supported_versions,
425             net::DefaultSupportedQuicVersions());
426 }
427 
TEST(URLRequestContextConfigTest,SetObsoleteQuicVersion)428 TEST(URLRequestContextConfigTest, SetObsoleteQuicVersion) {
429   // This test configures cronet with an obsolete QUIC version and validates
430   // that cronet ignores that version and uses the default versions.
431   base::test::TaskEnvironment task_environment_(
432       base::test::TaskEnvironment::MainThreadType::IO);
433 
434   std::unique_ptr<URLRequestContextConfig> config =
435       URLRequestContextConfig::CreateURLRequestContextConfig(
436           // Enable QUIC.
437           true,
438           // Enable SPDY.
439           true,
440           // Enable Brotli.
441           false,
442           // Type of http cache.
443           URLRequestContextConfig::HttpCacheType::DISK,
444           // Max size of http cache in bytes.
445           1024000,
446           // Disable caching for HTTP responses. Other information may be stored
447           // in the cache.
448           false,
449           // Storage path for http cache and cookie storage.
450           "/data/data/org.chromium.net/app_cronet_test/test_storage",
451           // Accept-Language request header field.
452           "foreign-language",
453           // User-Agent request header field.
454           "fake agent",
455           // JSON encoded experimental options.
456           std::string("{\"QUIC\":{\"quic_version\":\"") +
457               quic::ParsedQuicVersionToString(
458                   net::ObsoleteQuicVersions().back()) +
459               "\"}}",
460           // MockCertVerifier to use for testing purposes.
461           std::unique_ptr<net::CertVerifier>(),
462           // Enable network quality estimator.
463           false,
464           // Enable Public Key Pinning bypass for local trust anchors.
465           true,
466           // Optional network thread priority.
467           std::nullopt);
468 
469   net::URLRequestContextBuilder builder;
470   config->ConfigureURLRequestContextBuilder(&builder);
471   // Set a ProxyConfigService to avoid DCHECK failure when building.
472   builder.set_proxy_config_service(
473       std::make_unique<net::ProxyConfigServiceFixed>(
474           net::ProxyConfigWithAnnotation::CreateDirect()));
475   std::unique_ptr<net::URLRequestContext> context(builder.Build());
476   const net::QuicParams* quic_params = context->quic_context()->params();
477   EXPECT_EQ(quic_params->supported_versions,
478             net::DefaultSupportedQuicVersions());
479 }
480 
TEST(URLRequestContextConfigTest,SetQuicServerMigrationOptions)481 TEST(URLRequestContextConfigTest, SetQuicServerMigrationOptions) {
482   base::test::TaskEnvironment task_environment_(
483       base::test::TaskEnvironment::MainThreadType::IO);
484 
485   std::unique_ptr<URLRequestContextConfig> config =
486       URLRequestContextConfig::CreateURLRequestContextConfig(
487           // Enable QUIC.
488           true,
489           // Enable SPDY.
490           true,
491           // Enable Brotli.
492           false,
493           // Type of http cache.
494           URLRequestContextConfig::HttpCacheType::DISK,
495           // Max size of http cache in bytes.
496           1024000,
497           // Disable caching for HTTP responses. Other information may be stored
498           // in the cache.
499           false,
500           // Storage path for http cache and cookie storage.
501           "/data/data/org.chromium.net/app_cronet_test/test_storage",
502           // Accept-Language request header field.
503           "foreign-language",
504           // User-Agent request header field.
505           "fake agent",
506           // JSON encoded experimental options.
507           "{\"QUIC\":{\"allow_server_migration\":true}}",
508           // MockCertVerifier to use for testing purposes.
509           std::unique_ptr<net::CertVerifier>(),
510           // Enable network quality estimator.
511           false,
512           // Enable Public Key Pinning bypass for local trust anchors.
513           true,
514           // Optional network thread priority.
515           std::nullopt);
516 
517   net::URLRequestContextBuilder builder;
518   config->ConfigureURLRequestContextBuilder(&builder);
519   // Set a ProxyConfigService to avoid DCHECK failure when building.
520   builder.set_proxy_config_service(
521       std::make_unique<net::ProxyConfigServiceFixed>(
522           net::ProxyConfigWithAnnotation::CreateDirect()));
523   std::unique_ptr<net::URLRequestContext> context(builder.Build());
524   const net::QuicParams* quic_params = context->quic_context()->params();
525 
526   EXPECT_FALSE(quic_params->close_sessions_on_ip_change);
527   EXPECT_TRUE(quic_params->allow_server_migration);
528 }
529 
530 // Tests that goaway_sessions_on_ip_changes can be set on via
531 // experimental options.
TEST(URLRequestContextConfigTest,SetQuicGoAwaySessionsOnIPChangeViaExperimentOptions)532 TEST(URLRequestContextConfigTest,
533      SetQuicGoAwaySessionsOnIPChangeViaExperimentOptions) {
534   base::test::TaskEnvironment task_environment_(
535       base::test::TaskEnvironment::MainThreadType::IO);
536 
537   std::unique_ptr<URLRequestContextConfig> config =
538       URLRequestContextConfig::CreateURLRequestContextConfig(
539           // Enable QUIC.
540           true,
541           // Enable SPDY.
542           true,
543           // Enable Brotli.
544           false,
545           // Type of http cache.
546           URLRequestContextConfig::HttpCacheType::DISK,
547           // Max size of http cache in bytes.
548           1024000,
549           // Disable caching for HTTP responses. Other information may be stored
550           // in the cache.
551           false,
552           // Storage path for http cache and cookie storage.
553           "/data/data/org.chromium.net/app_cronet_test/test_storage",
554           // Accept-Language request header field.
555           "foreign-language",
556           // User-Agent request header field.
557           "fake agent",
558           // JSON encoded experimental options.
559           "{\"QUIC\":{\"goaway_sessions_on_ip_change\":true}}",
560           // MockCertVerifier to use for testing purposes.
561           std::unique_ptr<net::CertVerifier>(),
562           // Enable network quality estimator.
563           false,
564           // Enable Public Key Pinning bypass for local trust anchors.
565           true,
566           // Optional network thread priority.
567           std::nullopt);
568 
569   net::URLRequestContextBuilder builder;
570   config->ConfigureURLRequestContextBuilder(&builder);
571   // Set a ProxyConfigService to avoid DCHECK failure when building.
572   builder.set_proxy_config_service(
573       std::make_unique<net::ProxyConfigServiceFixed>(
574           net::ProxyConfigWithAnnotation::CreateDirect()));
575   std::unique_ptr<net::URLRequestContext> context(builder.Build());
576   const net::QuicParams* quic_params = context->quic_context()->params();
577 
578   EXPECT_FALSE(quic_params->close_sessions_on_ip_change);
579   EXPECT_TRUE(quic_params->goaway_sessions_on_ip_change);
580 }
581 
TEST(URLRequestContextConfigTest,SetQuicConnectionMigrationV2Options)582 TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationV2Options) {
583   base::test::TaskEnvironment task_environment_(
584       base::test::TaskEnvironment::MainThreadType::IO);
585 
586   std::unique_ptr<URLRequestContextConfig> config =
587       URLRequestContextConfig::CreateURLRequestContextConfig(
588           // Enable QUIC.
589           true,
590           // Enable SPDY.
591           true,
592           // Enable Brotli.
593           false,
594           // Type of http cache.
595           URLRequestContextConfig::HttpCacheType::DISK,
596           // Max size of http cache in bytes.
597           1024000,
598           // Disable caching for HTTP responses. Other information may be stored
599           // in the cache.
600           false,
601           // Storage path for http cache and cookie storage.
602           "/data/data/org.chromium.net/app_cronet_test/test_storage",
603           // Accept-Language request header field.
604           "foreign-language",
605           // User-Agent request header field.
606           "fake agent",
607           // JSON encoded experimental options.
608           "{\"QUIC\":{\"migrate_sessions_on_network_change_v2\":true,"
609           "\"migrate_sessions_early_v2\":true,"
610           "\"retry_on_alternate_network_before_handshake\":true,"
611           "\"migrate_idle_sessions\":true,"
612           "\"retransmittable_on_wire_timeout_milliseconds\":1000,"
613           "\"idle_session_migration_period_seconds\":15,"
614           "\"max_time_on_non_default_network_seconds\":10,"
615           "\"max_migrations_to_non_default_network_on_write_error\":3,"
616           "\"max_migrations_to_non_default_network_on_path_degrading\":4}}",
617           // MockCertVerifier to use for testing purposes.
618           std::unique_ptr<net::CertVerifier>(),
619           // Enable network quality estimator.
620           false,
621           // Enable Public Key Pinning bypass for local trust anchors.
622           true,
623           // Optional network thread priority.
624           std::nullopt);
625 
626   net::URLRequestContextBuilder builder;
627   config->ConfigureURLRequestContextBuilder(&builder);
628   // Set a ProxyConfigService to avoid DCHECK failure when building.
629   builder.set_proxy_config_service(
630       std::make_unique<net::ProxyConfigServiceFixed>(
631           net::ProxyConfigWithAnnotation::CreateDirect()));
632   std::unique_ptr<net::URLRequestContext> context(builder.Build());
633   const net::QuicParams* quic_params = context->quic_context()->params();
634 
635   EXPECT_TRUE(quic_params->migrate_sessions_on_network_change_v2);
636   EXPECT_TRUE(quic_params->migrate_sessions_early_v2);
637   EXPECT_TRUE(quic_params->retry_on_alternate_network_before_handshake);
638   EXPECT_EQ(1000,
639             quic_params->retransmittable_on_wire_timeout.InMilliseconds());
640   EXPECT_TRUE(quic_params->migrate_idle_sessions);
641   EXPECT_EQ(base::Seconds(15), quic_params->idle_session_migration_period);
642   EXPECT_EQ(base::Seconds(10), quic_params->max_time_on_non_default_network);
643   EXPECT_EQ(3,
644             quic_params->max_migrations_to_non_default_network_on_write_error);
645   EXPECT_EQ(
646       4, quic_params->max_migrations_to_non_default_network_on_path_degrading);
647   EXPECT_EQ(net::DefaultSupportedQuicVersions(),
648             quic_params->supported_versions);
649 }
650 
TEST(URLRequestContextConfigTest,SetQuicAllowPortMigration)651 TEST(URLRequestContextConfigTest, SetQuicAllowPortMigration) {
652   base::test::TaskEnvironment task_environment_(
653       base::test::TaskEnvironment::MainThreadType::IO);
654   std::unique_ptr<URLRequestContextConfig> config =
655       URLRequestContextConfig::CreateURLRequestContextConfig(
656           // Enable QUIC.
657           true,
658           // Enable SPDY.
659           true,
660           // Enable Brotli.
661           false,
662           // Type of http cache.
663           URLRequestContextConfig::HttpCacheType::DISK,
664           // Max size of http cache in bytes.
665           1024000,
666           // Disable caching for HTTP responses. Other information may be stored
667           // in the cache.
668           false,
669           // Storage path for http cache and cookie storage.
670           "/data/data/org.chromium.net/app_cronet_test/test_storage",
671           // Accept-Language request header field.
672           "foreign-language",
673           // User-Agent request header field.
674           "fake agent",
675           // JSON encoded experimental options.
676           "{\"QUIC\":{\"allow_port_migration\":false}}",
677           // MockCertVerifier to use for testing purposes.
678           std::unique_ptr<net::CertVerifier>(),
679           // Enable network quality estimator.
680           false,
681           // Enable Public Key Pinning bypass for local trust anchors.
682           true,
683           // Optional network thread priority.
684           std::nullopt);
685 
686   net::URLRequestContextBuilder builder;
687   config->ConfigureURLRequestContextBuilder(&builder);
688   // Set a ProxyConfigService to avoid DCHECK failure when building.
689   builder.set_proxy_config_service(
690       std::make_unique<net::ProxyConfigServiceFixed>(
691           net::ProxyConfigWithAnnotation::CreateDirect()));
692   std::unique_ptr<net::URLRequestContext> context(builder.Build());
693   const net::QuicParams* quic_params = context->quic_context()->params();
694 
695   EXPECT_FALSE(quic_params->allow_port_migration);
696 }
697 
TEST(URLRequestContextConfigTest,DisableQuicRetryWithoutAltSvcOnQuicErrors)698 TEST(URLRequestContextConfigTest, DisableQuicRetryWithoutAltSvcOnQuicErrors) {
699   base::test::TaskEnvironment task_environment_(
700       base::test::TaskEnvironment::MainThreadType::IO);
701   std::unique_ptr<URLRequestContextConfig> config =
702       URLRequestContextConfig::CreateURLRequestContextConfig(
703           // Enable QUIC.
704           true,
705           // Enable SPDY.
706           true,
707           // Enable Brotli.
708           false,
709           // Type of http cache.
710           URLRequestContextConfig::HttpCacheType::DISK,
711           // Max size of http cache in bytes.
712           1024000,
713           // Disable caching for HTTP responses. Other information may be stored
714           // in the cache.
715           false,
716           // Storage path for http cache and cookie storage.
717           "/data/data/org.chromium.net/app_cronet_test/test_storage",
718           // Accept-Language request header field.
719           "foreign-language",
720           // User-Agent request header field.
721           "fake agent",
722           // JSON encoded experimental options.
723           "{\"QUIC\":{\"retry_without_alt_svc_on_quic_errors\":false}}",
724           // MockCertVerifier to use for testing purposes.
725           std::unique_ptr<net::CertVerifier>(),
726           // Enable network quality estimator.
727           false,
728           // Enable Public Key Pinning bypass for local trust anchors.
729           true,
730           // Optional network thread priority.
731           std::nullopt);
732 
733   net::URLRequestContextBuilder builder;
734   config->ConfigureURLRequestContextBuilder(&builder);
735   // Set a ProxyConfigService to avoid DCHECK failure when building.
736   builder.set_proxy_config_service(
737       std::make_unique<net::ProxyConfigServiceFixed>(
738           net::ProxyConfigWithAnnotation::CreateDirect()));
739   std::unique_ptr<net::URLRequestContext> context(builder.Build());
740   const net::QuicParams* quic_params = context->quic_context()->params();
741 
742   EXPECT_FALSE(quic_params->retry_without_alt_svc_on_quic_errors);
743 }
744 
TEST(URLRequestContextConfigTest,BrokenAlternativeServiceDelayParams1)745 TEST(URLRequestContextConfigTest, BrokenAlternativeServiceDelayParams1) {
746   base::test::TaskEnvironment task_environment_(
747       base::test::TaskEnvironment::MainThreadType::IO);
748   std::unique_ptr<URLRequestContextConfig> config =
749       URLRequestContextConfig::CreateURLRequestContextConfig(
750           // Enable QUIC.
751           true,
752           // Enable SPDY.
753           true,
754           // Enable Brotli.
755           false,
756           // Type of http cache.
757           URLRequestContextConfig::HttpCacheType::DISK,
758           // Max size of http cache in bytes.
759           1024000,
760           // Disable caching for HTTP responses. Other information may be stored
761           // in the cache.
762           false,
763           // Storage path for http cache and cookie storage.
764           "/data/data/org.chromium.net/app_cronet_test/test_storage",
765           // Accept-Language request header field.
766           "foreign-language",
767           // User-Agent request header field.
768           "fake agent",
769           // JSON encoded experimental options.
770           "{\"QUIC\":{\"initial_delay_for_broken_alternative_service_seconds\":"
771           "1,"
772           "\"exponential_backoff_on_initial_delay\":true}}",
773           // MockCertVerifier to use for testing purposes.
774           std::unique_ptr<net::CertVerifier>(),
775           // Enable network quality estimator.
776           false,
777           // Enable Public Key Pinning bypass for local trust anchors.
778           true,
779           // Optional network thread priority.
780           std::nullopt);
781 
782   net::URLRequestContextBuilder builder;
783   config->ConfigureURLRequestContextBuilder(&builder);
784   // Set a ProxyConfigService to avoid DCHECK failure when building.
785   builder.set_proxy_config_service(
786       std::make_unique<net::ProxyConfigServiceFixed>(
787           net::ProxyConfigWithAnnotation::CreateDirect()));
788   std::unique_ptr<net::URLRequestContext> context(builder.Build());
789   const net::QuicParams* quic_params = context->quic_context()->params();
790 
791   ASSERT_TRUE(
792       quic_params->initial_delay_for_broken_alternative_service.has_value());
793   EXPECT_EQ(base::Seconds(1),
794             quic_params->initial_delay_for_broken_alternative_service.value());
795   ASSERT_TRUE(quic_params->exponential_backoff_on_initial_delay.has_value());
796   EXPECT_TRUE(quic_params->exponential_backoff_on_initial_delay.value());
797 }
798 
TEST(URLRequestContextConfigTest,BrokenAlternativeServiceDelayParams2)799 TEST(URLRequestContextConfigTest, BrokenAlternativeServiceDelayParams2) {
800   base::test::TaskEnvironment task_environment_(
801       base::test::TaskEnvironment::MainThreadType::IO);
802   std::unique_ptr<URLRequestContextConfig> config =
803       URLRequestContextConfig::CreateURLRequestContextConfig(
804           // Enable QUIC.
805           true,
806           // Enable SPDY.
807           true,
808           // Enable Brotli.
809           false,
810           // Type of http cache.
811           URLRequestContextConfig::HttpCacheType::DISK,
812           // Max size of http cache in bytes.
813           1024000,
814           // Disable caching for HTTP responses. Other information may be stored
815           // in the cache.
816           false,
817           // Storage path for http cache and cookie storage.
818           "/data/data/org.chromium.net/app_cronet_test/test_storage",
819           // Accept-Language request header field.
820           "foreign-language",
821           // User-Agent request header field.
822           "fake agent",
823           // JSON encoded experimental options.
824           "{\"QUIC\":{\"initial_delay_for_broken_alternative_service_seconds\":"
825           "5,"
826           "\"exponential_backoff_on_initial_delay\":false}}",
827           // MockCertVerifier to use for testing purposes.
828           std::unique_ptr<net::CertVerifier>(),
829           // Enable network quality estimator.
830           false,
831           // Enable Public Key Pinning bypass for local trust anchors.
832           true,
833           // Optional network thread priority.
834           std::nullopt);
835 
836   net::URLRequestContextBuilder builder;
837   config->ConfigureURLRequestContextBuilder(&builder);
838   // Set a ProxyConfigService to avoid DCHECK failure when building.
839   builder.set_proxy_config_service(
840       std::make_unique<net::ProxyConfigServiceFixed>(
841           net::ProxyConfigWithAnnotation::CreateDirect()));
842   std::unique_ptr<net::URLRequestContext> context(builder.Build());
843   const net::QuicParams* quic_params = context->quic_context()->params();
844 
845   ASSERT_TRUE(
846       quic_params->initial_delay_for_broken_alternative_service.has_value());
847   EXPECT_EQ(base::Seconds(5),
848             quic_params->initial_delay_for_broken_alternative_service.value());
849   ASSERT_TRUE(quic_params->exponential_backoff_on_initial_delay.has_value());
850   EXPECT_FALSE(quic_params->exponential_backoff_on_initial_delay.value());
851 }
852 
TEST(URLRequestContextConfigTest,DelayMainJobWithAvailableSpdySession)853 TEST(URLRequestContextConfigTest, DelayMainJobWithAvailableSpdySession) {
854   base::test::TaskEnvironment task_environment_(
855       base::test::TaskEnvironment::MainThreadType::IO);
856   std::unique_ptr<URLRequestContextConfig> config =
857       URLRequestContextConfig::CreateURLRequestContextConfig(
858           // Enable QUIC.
859           true,
860           // Enable SPDY.
861           true,
862           // Enable Brotli.
863           false,
864           // Type of http cache.
865           URLRequestContextConfig::HttpCacheType::DISK,
866           // Max size of http cache in bytes.
867           1024000,
868           // Disable caching for HTTP responses. Other information may be stored
869           // in the cache.
870           false,
871           // Storage path for http cache and cookie storage.
872           "/data/data/org.chromium.net/app_cronet_test/test_storage",
873           // Accept-Language request header field.
874           "foreign-language",
875           // User-Agent request header field.
876           "fake agent",
877           // JSON encoded experimental options.
878           "{\"QUIC\":{\"delay_main_job_with_available_spdy_session\":true}}",
879           // MockCertVerifier to use for testing purposes.
880           std::unique_ptr<net::CertVerifier>(),
881           // Enable network quality estimator.
882           false,
883           // Enable Public Key Pinning bypass for local trust anchors.
884           true,
885           // Optional network thread priority.
886           std::nullopt);
887 
888   net::URLRequestContextBuilder builder;
889   config->ConfigureURLRequestContextBuilder(&builder);
890   // Set a ProxyConfigService to avoid DCHECK failure when building.
891   builder.set_proxy_config_service(
892       std::make_unique<net::ProxyConfigServiceFixed>(
893           net::ProxyConfigWithAnnotation::CreateDirect()));
894   std::unique_ptr<net::URLRequestContext> context(builder.Build());
895   const net::QuicParams* quic_params = context->quic_context()->params();
896 
897   EXPECT_TRUE(quic_params->delay_main_job_with_available_spdy_session);
898 }
899 
TEST(URLRequestContextConfigTest,SetDisableTlsZeroRtt)900 TEST(URLRequestContextConfigTest, SetDisableTlsZeroRtt) {
901   base::test::TaskEnvironment task_environment_(
902       base::test::TaskEnvironment::MainThreadType::IO);
903   std::unique_ptr<URLRequestContextConfig> config =
904       URLRequestContextConfig::CreateURLRequestContextConfig(
905           // Enable QUIC.
906           true,
907           // Enable SPDY.
908           true,
909           // Enable Brotli.
910           false,
911           // Type of http cache.
912           URLRequestContextConfig::HttpCacheType::DISK,
913           // Max size of http cache in bytes.
914           1024000,
915           // Disable caching for HTTP responses. Other information may be stored
916           // in the cache.
917           false,
918           // Storage path for http cache and cookie storage.
919           "/data/data/org.chromium.net/app_cronet_test/test_storage",
920           // Accept-Language request header field.
921           "foreign-language",
922           // User-Agent request header field.
923           "fake agent",
924           // JSON encoded experimental options.
925           "{\"QUIC\":{\"disable_tls_zero_rtt\":true}}",
926           // MockCertVerifier to use for testing purposes.
927           std::unique_ptr<net::CertVerifier>(),
928           // Enable network quality estimator.
929           false,
930           // Enable Public Key Pinning bypass for local trust anchors.
931           true,
932           // Optional network thread priority.
933           std::nullopt);
934 
935   net::URLRequestContextBuilder builder;
936   config->ConfigureURLRequestContextBuilder(&builder);
937   // Set a ProxyConfigService to avoid DCHECK failure when building.
938   builder.set_proxy_config_service(
939       std::make_unique<net::ProxyConfigServiceFixed>(
940           net::ProxyConfigWithAnnotation::CreateDirect()));
941   std::unique_ptr<net::URLRequestContext> context(builder.Build());
942   const net::QuicParams* quic_params = context->quic_context()->params();
943 
944   EXPECT_TRUE(quic_params->disable_tls_zero_rtt);
945 }
946 
TEST(URLRequestContextConfigTest,SetQuicHostWhitelist)947 TEST(URLRequestContextConfigTest, SetQuicHostWhitelist) {
948   base::test::TaskEnvironment task_environment_(
949       base::test::TaskEnvironment::MainThreadType::IO);
950 
951   std::unique_ptr<URLRequestContextConfig> config =
952       URLRequestContextConfig::CreateURLRequestContextConfig(
953           // Enable QUIC.
954           true,
955           // Enable SPDY.
956           true,
957           // Enable Brotli.
958           false,
959           // Type of http cache.
960           URLRequestContextConfig::HttpCacheType::DISK,
961           // Max size of http cache in bytes.
962           1024000,
963           // Disable caching for HTTP responses. Other information may be stored
964           // in the cache.
965           false,
966           // Storage path for http cache and cookie storage.
967           "/data/data/org.chromium.net/app_cronet_test/test_storage",
968           // Accept-Language request header field.
969           "foreign-language",
970           // User-Agent request header field.
971           "fake agent",
972           // JSON encoded experimental options.
973           "{\"QUIC\":{\"host_whitelist\":\"www.example.com,www.example.org\"}}",
974           // MockCertVerifier to use for testing purposes.
975           std::unique_ptr<net::CertVerifier>(),
976           // Enable network quality estimator.
977           false,
978           // Enable Public Key Pinning bypass for local trust anchors.
979           true,
980           // Optional network thread priority.
981           std::nullopt);
982 
983   net::URLRequestContextBuilder builder;
984   config->ConfigureURLRequestContextBuilder(&builder);
985   // Set a ProxyConfigService to avoid DCHECK failure when building.
986   builder.set_proxy_config_service(
987       std::make_unique<net::ProxyConfigServiceFixed>(
988           net::ProxyConfigWithAnnotation::CreateDirect()));
989   std::unique_ptr<net::URLRequestContext> context(builder.Build());
990   const net::HttpNetworkSessionParams* params =
991       context->GetNetworkSessionParams();
992 
993   EXPECT_TRUE(base::Contains(params->quic_host_allowlist, "www.example.com"));
994   EXPECT_TRUE(base::Contains(params->quic_host_allowlist, "www.example.org"));
995 }
996 
TEST(URLRequestContextConfigTest,SetQuicMaxTimeBeforeCryptoHandshake)997 TEST(URLRequestContextConfigTest, SetQuicMaxTimeBeforeCryptoHandshake) {
998   base::test::TaskEnvironment task_environment_(
999       base::test::TaskEnvironment::MainThreadType::IO);
1000 
1001   std::unique_ptr<URLRequestContextConfig> config =
1002       URLRequestContextConfig::CreateURLRequestContextConfig(
1003           // Enable QUIC.
1004           true,
1005           // Enable SPDY.
1006           true,
1007           // Enable Brotli.
1008           false,
1009           // Type of http cache.
1010           URLRequestContextConfig::HttpCacheType::DISK,
1011           // Max size of http cache in bytes.
1012           1024000,
1013           // Disable caching for HTTP responses. Other information may be stored
1014           // in the cache.
1015           false,
1016           // Storage path for http cache and cookie storage.
1017           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1018           // Accept-Language request header field.
1019           "foreign-language",
1020           // User-Agent request header field.
1021           "fake agent",
1022           // JSON encoded experimental options.
1023           "{\"QUIC\":{\"max_time_before_crypto_handshake_seconds\":7,"
1024           "\"max_idle_time_before_crypto_handshake_seconds\":11}}",
1025           // MockCertVerifier to use for testing purposes.
1026           std::unique_ptr<net::CertVerifier>(),
1027           // Enable network quality estimator.
1028           false,
1029           // Enable Public Key Pinning bypass for local trust anchors.
1030           true,
1031           // Optional network thread priority.
1032           std::nullopt);
1033 
1034   net::URLRequestContextBuilder builder;
1035   config->ConfigureURLRequestContextBuilder(&builder);
1036   // Set a ProxyConfigService to avoid DCHECK failure when building.
1037   builder.set_proxy_config_service(
1038       std::make_unique<net::ProxyConfigServiceFixed>(
1039           net::ProxyConfigWithAnnotation::CreateDirect()));
1040   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1041   const net::QuicParams* quic_params = context->quic_context()->params();
1042 
1043   EXPECT_EQ(7, quic_params->max_time_before_crypto_handshake.InSeconds());
1044   EXPECT_EQ(11, quic_params->max_idle_time_before_crypto_handshake.InSeconds());
1045 }
1046 
TEST(URLURLRequestContextConfigTest,SetQuicConnectionOptions)1047 TEST(URLURLRequestContextConfigTest, SetQuicConnectionOptions) {
1048   base::test::TaskEnvironment task_environment_(
1049       base::test::TaskEnvironment::MainThreadType::IO);
1050 
1051   std::unique_ptr<URLRequestContextConfig> config =
1052       URLRequestContextConfig::CreateURLRequestContextConfig(
1053           // Enable QUIC.
1054           true,
1055           // Enable SPDY.
1056           true,
1057           // Enable Brotli.
1058           false,
1059           // Type of http cache.
1060           URLRequestContextConfig::HttpCacheType::DISK,
1061           // Max size of http cache in bytes.
1062           1024000,
1063           // Disable caching for HTTP responses. Other information may be stored
1064           // in the cache.
1065           false,
1066           // Storage path for http cache and cookie storage.
1067           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1068           // Accept-Language request header field.
1069           "foreign-language",
1070           // User-Agent request header field.
1071           "fake agent",
1072           // JSON encoded experimental options.
1073           "{\"QUIC\":{\"connection_options\":\"TIME,TBBR,REJ\","
1074           "\"client_connection_options\":\"TBBR,1RTT\"}}",
1075           // MockCertVerifier to use for testing purposes.
1076           std::unique_ptr<net::CertVerifier>(),
1077           // Enable network quality estimator.
1078           false,
1079           // Enable Public Key Pinning bypass for local trust anchors.
1080           true,
1081           // Optional network thread priority.
1082           std::nullopt);
1083 
1084   net::URLRequestContextBuilder builder;
1085   config->ConfigureURLRequestContextBuilder(&builder);
1086   // Set a ProxyConfigService to avoid DCHECK failure when building.
1087   builder.set_proxy_config_service(
1088       std::make_unique<net::ProxyConfigServiceFixed>(
1089           net::ProxyConfigWithAnnotation::CreateDirect()));
1090   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1091   const net::QuicParams* quic_params = context->quic_context()->params();
1092 
1093   quic::QuicTagVector connection_options;
1094   connection_options.push_back(quic::kTIME);
1095   connection_options.push_back(quic::kTBBR);
1096   connection_options.push_back(quic::kREJ);
1097   EXPECT_EQ(connection_options, quic_params->connection_options);
1098 
1099   quic::QuicTagVector client_connection_options;
1100   client_connection_options.push_back(quic::kTBBR);
1101   client_connection_options.push_back(quic::k1RTT);
1102   EXPECT_EQ(client_connection_options, quic_params->client_connection_options);
1103 }
1104 
TEST(URLURLRequestContextConfigTest,SetAcceptLanguageAndUserAgent)1105 TEST(URLURLRequestContextConfigTest, SetAcceptLanguageAndUserAgent) {
1106   base::test::TaskEnvironment task_environment_(
1107       base::test::TaskEnvironment::MainThreadType::IO);
1108 
1109   std::unique_ptr<URLRequestContextConfig> config =
1110       URLRequestContextConfig::CreateURLRequestContextConfig(
1111           // Enable QUIC.
1112           true,
1113           // Enable SPDY.
1114           true,
1115           // Enable Brotli.
1116           false,
1117           // Type of http cache.
1118           URLRequestContextConfig::HttpCacheType::DISK,
1119           // Max size of http cache in bytes.
1120           1024000,
1121           // Disable caching for HTTP responses. Other information may be stored
1122           // in the cache.
1123           false,
1124           // Storage path for http cache and cookie storage.
1125           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1126           // Accept-Language request header field.
1127           "foreign-language",
1128           // User-Agent request header field.
1129           "fake agent",
1130           // JSON encoded experimental options.
1131           "{}",
1132           // MockCertVerifier to use for testing purposes.
1133           std::unique_ptr<net::CertVerifier>(),
1134           // Enable network quality estimator.
1135           false,
1136           // Enable Public Key Pinning bypass for local trust anchors.
1137           true,
1138           // Optional network thread priority.
1139           std::nullopt);
1140 
1141   net::URLRequestContextBuilder builder;
1142   config->ConfigureURLRequestContextBuilder(&builder);
1143   // Set a ProxyConfigService to avoid DCHECK failure when building.
1144   builder.set_proxy_config_service(
1145       std::make_unique<net::ProxyConfigServiceFixed>(
1146           net::ProxyConfigWithAnnotation::CreateDirect()));
1147   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1148   EXPECT_EQ("foreign-language",
1149             context->http_user_agent_settings()->GetAcceptLanguage());
1150   EXPECT_EQ("fake agent", context->http_user_agent_settings()->GetUserAgent());
1151 }
1152 
TEST(URLURLRequestContextConfigTest,TurningOffQuic)1153 TEST(URLURLRequestContextConfigTest, TurningOffQuic) {
1154   base::test::TaskEnvironment task_environment_(
1155       base::test::TaskEnvironment::MainThreadType::IO);
1156 
1157   std::unique_ptr<URLRequestContextConfig> config =
1158       URLRequestContextConfig::CreateURLRequestContextConfig(
1159           // Enable QUIC.
1160           false,
1161           // Enable SPDY.
1162           true,
1163           // Enable Brotli.
1164           false,
1165           // Type of http cache.
1166           URLRequestContextConfig::HttpCacheType::DISK,
1167           // Max size of http cache in bytes.
1168           1024000,
1169           // Disable caching for HTTP responses. Other information may be stored
1170           // in the cache.
1171           false,
1172           // Storage path for http cache and cookie storage.
1173           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1174           // Accept-Language request header field.
1175           "foreign-language",
1176           // User-Agent request header field.
1177           "fake agent",
1178           // JSON encoded experimental options.
1179           "{}",
1180           // MockCertVerifier to use for testing purposes.
1181           std::unique_ptr<net::CertVerifier>(),
1182           // Enable network quality estimator.
1183           false,
1184           // Enable Public Key Pinning bypass for local trust anchors.
1185           true,
1186           // Optional network thread priority.
1187           std::nullopt);
1188 
1189   net::URLRequestContextBuilder builder;
1190   config->ConfigureURLRequestContextBuilder(&builder);
1191   // Set a ProxyConfigService to avoid DCHECK failure when building.
1192   builder.set_proxy_config_service(
1193       std::make_unique<net::ProxyConfigServiceFixed>(
1194           net::ProxyConfigWithAnnotation::CreateDirect()));
1195   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1196   const net::HttpNetworkSessionParams* params =
1197       context->GetNetworkSessionParams();
1198   EXPECT_EQ(false, params->enable_quic);
1199 }
1200 
TEST(URLURLRequestContextConfigTest,DefaultEnableQuic)1201 TEST(URLURLRequestContextConfigTest, DefaultEnableQuic) {
1202   base::test::TaskEnvironment task_environment_(
1203       base::test::TaskEnvironment::MainThreadType::IO);
1204 
1205   URLRequestContextConfigBuilder config_builder;
1206   std::unique_ptr<URLRequestContextConfig> config = config_builder.Build();
1207   net::URLRequestContextBuilder builder;
1208   config->ConfigureURLRequestContextBuilder(&builder);
1209   // Set a ProxyConfigService to avoid DCHECK failure when building.
1210   builder.set_proxy_config_service(
1211       std::make_unique<net::ProxyConfigServiceFixed>(
1212           net::ProxyConfigWithAnnotation::CreateDirect()));
1213   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1214   const net::HttpNetworkSessionParams* params =
1215       context->GetNetworkSessionParams();
1216   EXPECT_EQ(true, params->enable_quic);
1217 }
1218 
TEST(URLRequestContextConfigTest,SetSpdyGoAwayOnIPChange)1219 TEST(URLRequestContextConfigTest, SetSpdyGoAwayOnIPChange) {
1220   base::test::TaskEnvironment task_environment_(
1221       base::test::TaskEnvironment::MainThreadType::IO);
1222 
1223   std::unique_ptr<URLRequestContextConfig> config =
1224       URLRequestContextConfig::CreateURLRequestContextConfig(
1225           // Enable QUIC.
1226           true,
1227           // Enable SPDY.
1228           true,
1229           // Enable Brotli.
1230           false,
1231           // Type of http cache.
1232           URLRequestContextConfig::HttpCacheType::DISK,
1233           // Max size of http cache in bytes.
1234           1024000,
1235           // Disable caching for HTTP responses. Other information may be stored
1236           // in the cache.
1237           false,
1238           // Storage path for http cache and cookie storage.
1239           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1240           // Accept-Language request header field.
1241           "foreign-language",
1242           // User-Agent request header field.
1243           "fake agent",
1244           // JSON encoded experimental options.
1245           "{\"spdy_go_away_on_ip_change\":false}",
1246           // MockCertVerifier to use for testing purposes.
1247           std::unique_ptr<net::CertVerifier>(),
1248           // Enable network quality estimator.
1249           false,
1250           // Enable Public Key Pinning bypass for local trust anchors.
1251           true,
1252           // Optional network thread priority.
1253           std::nullopt);
1254 
1255   net::URLRequestContextBuilder builder;
1256   config->ConfigureURLRequestContextBuilder(&builder);
1257   // Set a ProxyConfigService to avoid DCHECK failure when building.
1258   builder.set_proxy_config_service(
1259       std::make_unique<net::ProxyConfigServiceFixed>(
1260           net::ProxyConfigWithAnnotation::CreateDirect()));
1261   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1262   const net::HttpNetworkSessionParams* params =
1263       context->GetNetworkSessionParams();
1264   EXPECT_FALSE(params->spdy_go_away_on_ip_change);
1265 }
1266 
TEST(URLRequestContextConfigTest,WrongSpdyGoAwayOnIPChangeValue)1267 TEST(URLRequestContextConfigTest, WrongSpdyGoAwayOnIPChangeValue) {
1268   base::test::TaskEnvironment task_environment_(
1269       base::test::TaskEnvironment::MainThreadType::IO);
1270 
1271   std::unique_ptr<URLRequestContextConfig> config =
1272       URLRequestContextConfig::CreateURLRequestContextConfig(
1273           // Enable QUIC.
1274           true,
1275           // Enable SPDY.
1276           true,
1277           // Enable Brotli.
1278           false,
1279           // Type of http cache.
1280           URLRequestContextConfig::HttpCacheType::DISK,
1281           // Max size of http cache in bytes.
1282           1024000,
1283           // Disable caching for HTTP responses. Other information may be stored
1284           // in the cache.
1285           false,
1286           // Storage path for http cache and cookie storage.
1287           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1288           // Accept-Language request header field.
1289           "foreign-language",
1290           // User-Agent request header field.
1291           "fake agent",
1292           // JSON encoded experimental options.
1293           "{\"spdy_go_away_on_ip_change\":\"not a bool\"}",
1294           // MockCertVerifier to use for testing purposes.
1295           std::unique_ptr<net::CertVerifier>(),
1296           // Enable network quality estimator.
1297           false,
1298           // Enable Public Key Pinning bypass for local trust anchors.
1299           true,
1300           // Optional network thread priority.
1301           std::nullopt);
1302 
1303   net::URLRequestContextBuilder builder;
1304   config->ConfigureURLRequestContextBuilder(&builder);
1305   EXPECT_FALSE(config->effective_experimental_options.contains(
1306       "spdy_go_away_on_ip_change"));
1307 }
1308 
TEST(URLRequestContextConfigTest,BidiStreamDetectBrokenConnection)1309 TEST(URLRequestContextConfigTest, BidiStreamDetectBrokenConnection) {
1310   base::test::TaskEnvironment task_environment_(
1311       base::test::TaskEnvironment::MainThreadType::IO);
1312 
1313   std::unique_ptr<URLRequestContextConfig> config =
1314       URLRequestContextConfig::CreateURLRequestContextConfig(
1315           // Enable QUIC.
1316           true,
1317           // Enable SPDY.
1318           true,
1319           // Enable Brotli.
1320           false,
1321           // Type of http cache.
1322           URLRequestContextConfig::HttpCacheType::DISK,
1323           // Max size of http cache in bytes.
1324           1024000,
1325           // Disable caching for HTTP responses. Other information may be stored
1326           // in the cache.
1327           false,
1328           // Storage path for http cache and cookie storage.
1329           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1330           // Accept-Language request header field.
1331           "foreign-language",
1332           // User-Agent request header field.
1333           "fake agent",
1334           // JSON encoded experimental options.
1335           "{\"bidi_stream_detect_broken_connection\":10}",
1336           // MockCertVerifier to use for testing purposes.
1337           std::unique_ptr<net::CertVerifier>(),
1338           // Enable network quality estimator.
1339           false,
1340           // Enable Public Key Pinning bypass for local trust anchors.
1341           true,
1342           // Optional network thread priority.
1343           std::nullopt);
1344 
1345   net::URLRequestContextBuilder builder;
1346   config->ConfigureURLRequestContextBuilder(&builder);
1347   EXPECT_TRUE(config->effective_experimental_options.contains(
1348       "bidi_stream_detect_broken_connection"));
1349   EXPECT_TRUE(config->bidi_stream_detect_broken_connection);
1350   EXPECT_EQ(config->heartbeat_interval, base::Seconds(10));
1351 }
1352 
TEST(URLRequestContextConfigTest,WrongBidiStreamDetectBrokenConnectionValue)1353 TEST(URLRequestContextConfigTest, WrongBidiStreamDetectBrokenConnectionValue) {
1354   base::test::TaskEnvironment task_environment_(
1355       base::test::TaskEnvironment::MainThreadType::IO);
1356 
1357   std::unique_ptr<URLRequestContextConfig> config =
1358       URLRequestContextConfig::CreateURLRequestContextConfig(
1359           // Enable QUIC.
1360           true,
1361           // Enable SPDY.
1362           true,
1363           // Enable Brotli.
1364           false,
1365           // Type of http cache.
1366           URLRequestContextConfig::HttpCacheType::DISK,
1367           // Max size of http cache in bytes.
1368           1024000,
1369           // Disable caching for HTTP responses. Other information may be stored
1370           // in the cache.
1371           false,
1372           // Storage path for http cache and cookie storage.
1373           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1374           // Accept-Language request header field.
1375           "foreign-language",
1376           // User-Agent request header field.
1377           "fake agent",
1378           // JSON encoded experimental options.
1379           "{\"bidi_stream_detect_broken_connection\": \"not an int\"}",
1380           // MockCertVerifier to use for testing purposes.
1381           std::unique_ptr<net::CertVerifier>(),
1382           // Enable network quality estimator.
1383           false,
1384           // Enable Public Key Pinning bypass for local trust anchors.
1385           true,
1386           // Optional network thread priority.
1387           std::nullopt);
1388 
1389   net::URLRequestContextBuilder builder;
1390   config->ConfigureURLRequestContextBuilder(&builder);
1391   EXPECT_FALSE(config->effective_experimental_options.contains(
1392       "bidi_stream_detect_broken_connection"));
1393 }
1394 
TEST(URLRequestContextConfigTest,HttpsSvcbOptions)1395 TEST(URLRequestContextConfigTest, HttpsSvcbOptions) {
1396   base::test::TaskEnvironment task_environment_(
1397       base::test::TaskEnvironment::MainThreadType::IO);
1398 
1399   std::unique_ptr<URLRequestContextConfig> config =
1400       URLRequestContextConfig::CreateURLRequestContextConfig(
1401           // Enable QUIC.
1402           true,
1403           // Enable SPDY.
1404           true,
1405           // Enable Brotli.
1406           false,
1407           // Type of http cache.
1408           URLRequestContextConfig::HttpCacheType::DISK,
1409           // Max size of http cache in bytes.
1410           1024000,
1411           // Disable caching for HTTP responses. Other information may be stored
1412           // in the cache.
1413           false,
1414           // Storage path for http cache and cookie storage.
1415           "/data/data/org.chromium.net/app_cronet_test/test_storage",
1416           // Accept-Language request header field.
1417           "foreign-language",
1418           // User-Agent request header field.
1419           "fake agent",
1420           // JSON encoded experimental options.
1421           "{\"UseDnsHttpsSvcb\":{\"enable\":true,"
1422           "\"insecure_extra_time_max\":\"1ms\","
1423           "\"insecure_extra_time_percent\":2,"
1424           "\"insecure_extra_time_min\":\"3ms\","
1425           "\"secure_extra_time_max\":\"4ms\","
1426           "\"secure_extra_time_percent\":5,"
1427           "\"secure_extra_time_min\":\"6ms\","
1428           "\"use_alpn\":true"
1429           "}}",
1430           // MockCertVerifier to use for testing purposes.
1431           std::unique_ptr<net::CertVerifier>(),
1432           // Enable network quality estimator.
1433           false,
1434           // Enable Public Key Pinning bypass for local trust anchors.
1435           true,
1436           // Optional network thread priority.
1437           std::nullopt);
1438   net::URLRequestContextBuilder builder;
1439   config->ConfigureURLRequestContextBuilder(&builder);
1440   // Set a ProxyConfigService to avoid DCHECK failure when building.
1441   builder.set_proxy_config_service(
1442       std::make_unique<net::ProxyConfigServiceFixed>(
1443           net::ProxyConfigWithAnnotation::CreateDirect()));
1444   std::unique_ptr<net::URLRequestContext> context(builder.Build());
1445 
1446   const net::HostResolver::HttpsSvcbOptions& https_svcb_options =
1447       context->host_resolver()
1448           ->GetManagerForTesting()
1449           ->https_svcb_options_for_testing();
1450   EXPECT_TRUE(https_svcb_options.enable);
1451   EXPECT_EQ(base::Milliseconds(1), https_svcb_options.insecure_extra_time_max);
1452   EXPECT_EQ(2, https_svcb_options.insecure_extra_time_percent);
1453   EXPECT_EQ(base::Milliseconds(3), https_svcb_options.insecure_extra_time_min);
1454   EXPECT_EQ(base::Milliseconds(4), https_svcb_options.secure_extra_time_max);
1455   EXPECT_EQ(5, https_svcb_options.secure_extra_time_percent);
1456   EXPECT_EQ(base::Milliseconds(6), https_svcb_options.secure_extra_time_min);
1457 
1458   const net::HttpNetworkSessionParams* params =
1459       context->GetNetworkSessionParams();
1460   EXPECT_TRUE(params->use_dns_https_svcb_alpn);
1461 }
1462 
1463 // See stale_host_resolver_unittest.cc for test of StaleDNS options.
1464 
1465 }  // namespace cronet
1466