1 // Copyright 2012 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 "net/url_request/url_request_context_builder.h"
6
7 #include "base/functional/callback_helpers.h"
8 #include "base/run_loop.h"
9 #include "base/task/single_thread_task_runner.h"
10 #include "base/task/thread_pool.h"
11 #include "base/test/scoped_feature_list.h"
12 #include "build/build_config.h"
13 #include "net/base/cronet_buildflags.h"
14 #include "net/base/mock_network_change_notifier.h"
15 #include "net/base/network_anonymization_key.h"
16 #include "net/base/request_priority.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/dns/host_resolver_manager.h"
20 #include "net/dns/mock_host_resolver.h"
21 #include "net/http/http_auth_challenge_tokenizer.h"
22 #include "net/http/http_auth_handler.h"
23 #include "net/http/http_auth_handler_factory.h"
24 #include "net/log/net_log_with_source.h"
25 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
26 #include "net/socket/client_socket_factory.h"
27 #include "net/ssl/ssl_info.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 #include "net/test/gtest_util.h"
30 #include "net/test/test_with_task_environment.h"
31 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
32 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "testing/platform_test.h"
36 #include "url/gurl.h"
37 #include "url/scheme_host_port.h"
38
39 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
40 #include "net/proxy_resolution/proxy_config.h"
41 #include "net/proxy_resolution/proxy_config_service_fixed.h"
42 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
43 // BUILDFLAG(IS_ANDROID)
44
45 #if BUILDFLAG(IS_ANDROID)
46 #include "base/android/build_info.h"
47 #endif // BUILDFLAG(IS_ANDROID)
48
49 #if BUILDFLAG(ENABLE_REPORTING)
50 #include "base/files/scoped_temp_dir.h"
51
52 #if !BUILDFLAG(CRONET_BUILD)
53 // gn check does not account for BUILDFLAG(). So, for Cronet builds, it will
54 // complain about a missing dependency on the target exposing this header. Add a
55 // nogncheck to stop it from yelling.
56 #include "net/extras/sqlite/sqlite_persistent_reporting_and_nel_store.h" // nogncheck
57 #endif // !BUILDFLAG(CRONET_BUILD)
58
59 #include "net/reporting/reporting_context.h"
60 #include "net/reporting/reporting_policy.h"
61 #include "net/reporting/reporting_service.h"
62 #include "net/reporting/reporting_uploader.h"
63 #endif // BUILDFLAG(ENABLE_REPORTING)
64
65 namespace net {
66
67 namespace {
68
69 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory {
70 public:
MockHttpAuthHandlerFactory(std::string supported_scheme,int return_code)71 MockHttpAuthHandlerFactory(std::string supported_scheme, int return_code)
72 : return_code_(return_code), supported_scheme_(supported_scheme) {}
73 ~MockHttpAuthHandlerFactory() override = default;
74
CreateAuthHandler(HttpAuthChallengeTokenizer * challenge,HttpAuth::Target target,const SSLInfo & ssl_info,const NetworkAnonymizationKey & network_anonymization_key,const url::SchemeHostPort & scheme_host_port,CreateReason reason,int nonce_count,const NetLogWithSource & net_log,HostResolver * host_resolver,std::unique_ptr<HttpAuthHandler> * handler)75 int CreateAuthHandler(
76 HttpAuthChallengeTokenizer* challenge,
77 HttpAuth::Target target,
78 const SSLInfo& ssl_info,
79 const NetworkAnonymizationKey& network_anonymization_key,
80 const url::SchemeHostPort& scheme_host_port,
81 CreateReason reason,
82 int nonce_count,
83 const NetLogWithSource& net_log,
84 HostResolver* host_resolver,
85 std::unique_ptr<HttpAuthHandler>* handler) override {
86 handler->reset();
87
88 return challenge->auth_scheme() == supported_scheme_
89 ? return_code_
90 : ERR_UNSUPPORTED_AUTH_SCHEME;
91 }
92
93 private:
94 int return_code_;
95 std::string supported_scheme_;
96 };
97
98 class URLRequestContextBuilderTest : public PlatformTest,
99 public WithTaskEnvironment {
100 protected:
URLRequestContextBuilderTest()101 URLRequestContextBuilderTest() {
102 test_server_.AddDefaultHandlers(
103 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
104 SetUpURLRequestContextBuilder(builder_);
105 }
106
SetUpURLRequestContextBuilder(URLRequestContextBuilder & builder)107 void SetUpURLRequestContextBuilder(URLRequestContextBuilder& builder) {
108 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
109 builder.set_proxy_config_service(std::make_unique<ProxyConfigServiceFixed>(
110 ProxyConfigWithAnnotation::CreateDirect()));
111 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
112 // BUILDFLAG(IS_ANDROID)
113 }
114
115 std::unique_ptr<HostResolver> host_resolver_ =
116 std::make_unique<MockHostResolver>();
117 EmbeddedTestServer test_server_;
118 URLRequestContextBuilder builder_;
119 };
120
TEST_F(URLRequestContextBuilderTest,DefaultSettings)121 TEST_F(URLRequestContextBuilderTest, DefaultSettings) {
122 ASSERT_TRUE(test_server_.Start());
123
124 std::unique_ptr<URLRequestContext> context(builder_.Build());
125 TestDelegate delegate;
126 std::unique_ptr<URLRequest> request(context->CreateRequest(
127 test_server_.GetURL("/echoheader?Foo"), DEFAULT_PRIORITY, &delegate,
128 TRAFFIC_ANNOTATION_FOR_TESTS));
129 request->set_method("GET");
130 request->SetExtraRequestHeaderByName("Foo", "Bar", false);
131 request->Start();
132 delegate.RunUntilComplete();
133 EXPECT_EQ("Bar", delegate.data_received());
134 }
135
TEST_F(URLRequestContextBuilderTest,UserAgent)136 TEST_F(URLRequestContextBuilderTest, UserAgent) {
137 ASSERT_TRUE(test_server_.Start());
138
139 builder_.set_user_agent("Bar");
140 std::unique_ptr<URLRequestContext> context(builder_.Build());
141 TestDelegate delegate;
142 std::unique_ptr<URLRequest> request(context->CreateRequest(
143 test_server_.GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY,
144 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
145 request->set_method("GET");
146 request->Start();
147 delegate.RunUntilComplete();
148 EXPECT_EQ("Bar", delegate.data_received());
149 }
150
TEST_F(URLRequestContextBuilderTest,DefaultHttpAuthHandlerFactory)151 TEST_F(URLRequestContextBuilderTest, DefaultHttpAuthHandlerFactory) {
152 url::SchemeHostPort scheme_host_port(GURL("https://www.google.com"));
153 std::unique_ptr<HttpAuthHandler> handler;
154 std::unique_ptr<URLRequestContext> context(builder_.Build());
155 SSLInfo null_ssl_info;
156
157 // Verify that the default basic handler is present
158 EXPECT_EQ(OK,
159 context->http_auth_handler_factory()->CreateAuthHandlerFromString(
160 "basic", HttpAuth::AUTH_SERVER, null_ssl_info,
161 NetworkAnonymizationKey(), scheme_host_port, NetLogWithSource(),
162 host_resolver_.get(), &handler));
163 }
164
TEST_F(URLRequestContextBuilderTest,CustomHttpAuthHandlerFactory)165 TEST_F(URLRequestContextBuilderTest, CustomHttpAuthHandlerFactory) {
166 url::SchemeHostPort scheme_host_port(GURL("https://www.google.com"));
167 const int kBasicReturnCode = OK;
168 std::unique_ptr<HttpAuthHandler> handler;
169 builder_.SetHttpAuthHandlerFactory(
170 std::make_unique<MockHttpAuthHandlerFactory>("extrascheme",
171 kBasicReturnCode));
172 std::unique_ptr<URLRequestContext> context(builder_.Build());
173 SSLInfo null_ssl_info;
174 // Verify that a handler is returned for a custom scheme.
175 EXPECT_EQ(kBasicReturnCode,
176 context->http_auth_handler_factory()->CreateAuthHandlerFromString(
177 "ExtraScheme", HttpAuth::AUTH_SERVER, null_ssl_info,
178 NetworkAnonymizationKey(), scheme_host_port, NetLogWithSource(),
179 host_resolver_.get(), &handler));
180
181 // Verify that the default basic handler isn't present
182 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME,
183 context->http_auth_handler_factory()->CreateAuthHandlerFromString(
184 "basic", HttpAuth::AUTH_SERVER, null_ssl_info,
185 NetworkAnonymizationKey(), scheme_host_port, NetLogWithSource(),
186 host_resolver_.get(), &handler));
187
188 // Verify that a handler isn't returned for a bogus scheme.
189 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME,
190 context->http_auth_handler_factory()->CreateAuthHandlerFromString(
191 "Bogus", HttpAuth::AUTH_SERVER, null_ssl_info,
192 NetworkAnonymizationKey(), scheme_host_port, NetLogWithSource(),
193 host_resolver_.get(), &handler));
194 }
195
196 #if BUILDFLAG(ENABLE_REPORTING)
197 // See crbug.com/935209. This test ensures that shutdown occurs correctly and
198 // does not crash while destoying the NEL and Reporting services in the process
199 // of destroying the URLRequestContext whilst Reporting has a pending upload.
TEST_F(URLRequestContextBuilderTest,ShutDownNELAndReportingWithPendingUpload)200 TEST_F(URLRequestContextBuilderTest, ShutDownNELAndReportingWithPendingUpload) {
201 std::unique_ptr<MockHostResolver> host_resolver =
202 std::make_unique<MockHostResolver>();
203 host_resolver->set_ondemand_mode(true);
204 MockHostResolver* mock_host_resolver = host_resolver.get();
205 builder_.set_host_resolver(std::move(host_resolver));
206 builder_.set_proxy_resolution_service(
207 ConfiguredProxyResolutionService::CreateDirect());
208 builder_.set_reporting_policy(std::make_unique<ReportingPolicy>());
209 builder_.set_network_error_logging_enabled(true);
210
211 std::unique_ptr<URLRequestContext> context(builder_.Build());
212 ASSERT_TRUE(context->network_error_logging_service());
213 ASSERT_TRUE(context->reporting_service());
214
215 // Queue a pending upload.
216 GURL url("https://www.foo.test");
217 context->reporting_service()->GetContextForTesting()->uploader()->StartUpload(
218 url::Origin::Create(url), url, IsolationInfo::CreateTransient(),
219 "report body", 0,
220 /*eligible_for_credentials=*/false, base::DoNothing());
221 base::RunLoop().RunUntilIdle();
222 ASSERT_EQ(1, context->reporting_service()
223 ->GetContextForTesting()
224 ->uploader()
225 ->GetPendingUploadCountForTesting());
226 ASSERT_TRUE(mock_host_resolver->has_pending_requests());
227
228 // This should shut down and destroy the NEL and Reporting services, including
229 // the PendingUpload, and should not cause a crash.
230 context.reset();
231 }
232
233 #if !BUILDFLAG(CRONET_BUILD)
234 // See crbug.com/935209. This test ensures that shutdown occurs correctly and
235 // does not crash while destoying the NEL and Reporting services in the process
236 // of destroying the URLRequestContext whilst Reporting has a pending upload.
TEST_F(URLRequestContextBuilderTest,ShutDownNELAndReportingWithPendingUploadAndPersistentStorage)237 TEST_F(URLRequestContextBuilderTest,
238 ShutDownNELAndReportingWithPendingUploadAndPersistentStorage) {
239 std::unique_ptr<MockHostResolver> host_resolver =
240 std::make_unique<MockHostResolver>();
241 host_resolver->set_ondemand_mode(true);
242 MockHostResolver* mock_host_resolver = host_resolver.get();
243 builder_.set_host_resolver(std::move(host_resolver));
244 builder_.set_proxy_resolution_service(
245 ConfiguredProxyResolutionService::CreateDirect());
246 builder_.set_reporting_policy(std::make_unique<ReportingPolicy>());
247 builder_.set_network_error_logging_enabled(true);
248 base::ScopedTempDir scoped_temp_dir;
249 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
250 builder_.set_persistent_reporting_and_nel_store(
251 std::make_unique<SQLitePersistentReportingAndNelStore>(
252 scoped_temp_dir.GetPath().Append(
253 FILE_PATH_LITERAL("ReportingAndNelStore")),
254 base::SingleThreadTaskRunner::GetCurrentDefault(),
255 base::ThreadPool::CreateSequencedTaskRunner(
256 {base::MayBlock(),
257 net::GetReportingAndNelStoreBackgroundSequencePriority(),
258 base::TaskShutdownBehavior::BLOCK_SHUTDOWN})));
259
260 std::unique_ptr<URLRequestContext> context(builder_.Build());
261 ASSERT_TRUE(context->network_error_logging_service());
262 ASSERT_TRUE(context->reporting_service());
263 ASSERT_TRUE(context->network_error_logging_service()
264 ->GetPersistentNelStoreForTesting());
265 ASSERT_TRUE(context->reporting_service()->GetContextForTesting()->store());
266
267 // Queue a pending upload.
268 GURL url("https://www.foo.test");
269 context->reporting_service()->GetContextForTesting()->uploader()->StartUpload(
270 url::Origin::Create(url), url, IsolationInfo::CreateTransient(),
271 "report body", 0,
272 /*eligible_for_credentials=*/false, base::DoNothing());
273 base::RunLoop().RunUntilIdle();
274 ASSERT_EQ(1, context->reporting_service()
275 ->GetContextForTesting()
276 ->uploader()
277 ->GetPendingUploadCountForTesting());
278 ASSERT_TRUE(mock_host_resolver->has_pending_requests());
279
280 // This should shut down and destroy the NEL and Reporting services, including
281 // the PendingUpload, and should not cause a crash.
282 context.reset();
283 }
284 #endif // !BUILDFLAG(CRONET_BUILD)
285
TEST_F(URLRequestContextBuilderTest,BuilderSetEnterpriseReportingEndpointsWithFeatureEnabled)286 TEST_F(URLRequestContextBuilderTest,
287 BuilderSetEnterpriseReportingEndpointsWithFeatureEnabled) {
288 base::test::ScopedFeatureList feature_list;
289 feature_list.InitAndEnableFeature(
290 net::features::kReportingApiEnableEnterpriseCookieIssues);
291 base::flat_map<std::string, GURL> test_enterprise_endpoints{
292 {"endpoint-1", GURL("https://example.com/reports")},
293 {"endpoint-2", GURL("https://reporting.example/cookie-issues")},
294 {"endpoint-3", GURL("https://report-collector.example")},
295 };
296 builder_.set_reporting_policy(std::make_unique<ReportingPolicy>());
297 builder_.set_enterprise_reporting_endpoints(test_enterprise_endpoints);
298 std::unique_ptr<URLRequestContext> context(builder_.Build());
299 ASSERT_TRUE(context->reporting_service());
300 std::vector<net::ReportingEndpoint> expected_enterprise_endpoints = {
301 {net::ReportingEndpointGroupKey(net::NetworkAnonymizationKey(),
302 /*reporting_source=*/std::nullopt,
303 /*origin=*/std::nullopt, "endpoint-1",
304 net::ReportingTargetType::kEnterprise),
305 {.url = GURL("https://example.com/reports")}},
306 {net::ReportingEndpointGroupKey(net::NetworkAnonymizationKey(),
307 /*reporting_source=*/std::nullopt,
308 /*origin=*/std::nullopt, "endpoint-2",
309 net::ReportingTargetType::kEnterprise),
310 {.url = GURL("https://reporting.example/cookie-issues")}},
311 {net::ReportingEndpointGroupKey(net::NetworkAnonymizationKey(),
312 /*reporting_source=*/std::nullopt,
313 /*origin=*/std::nullopt, "endpoint-3",
314 net::ReportingTargetType::kEnterprise),
315 {.url = GURL("https://report-collector.example")}}};
316
317 EXPECT_EQ(expected_enterprise_endpoints,
318 context->reporting_service()
319 ->GetContextForTesting()
320 ->cache()
321 ->GetEnterpriseEndpointsForTesting());
322 }
323
TEST_F(URLRequestContextBuilderTest,BuilderSetEnterpriseReportingEndpointsWithFeatureDisabled)324 TEST_F(URLRequestContextBuilderTest,
325 BuilderSetEnterpriseReportingEndpointsWithFeatureDisabled) {
326 base::test::ScopedFeatureList feature_list;
327 feature_list.InitAndDisableFeature(
328 net::features::kReportingApiEnableEnterpriseCookieIssues);
329 base::flat_map<std::string, GURL> test_enterprise_endpoints{
330 {"endpoint-1", GURL("https://example.com/reports")},
331 {"endpoint-2", GURL("https://reporting.example/cookie-issues")},
332 {"endpoint-3", GURL("https://report-collector.example")},
333 };
334 builder_.set_reporting_policy(std::make_unique<ReportingPolicy>());
335 builder_.set_enterprise_reporting_endpoints(test_enterprise_endpoints);
336 std::unique_ptr<URLRequestContext> context(builder_.Build());
337 ASSERT_TRUE(context->reporting_service());
338
339 EXPECT_EQ(0u, context->reporting_service()
340 ->GetContextForTesting()
341 ->cache()
342 ->GetEnterpriseEndpointsForTesting()
343 .size());
344 }
345 #endif // BUILDFLAG(ENABLE_REPORTING)
346
TEST_F(URLRequestContextBuilderTest,ShutdownHostResolverWithPendingRequest)347 TEST_F(URLRequestContextBuilderTest, ShutdownHostResolverWithPendingRequest) {
348 auto mock_host_resolver = std::make_unique<MockHostResolver>();
349 mock_host_resolver->rules()->AddRule("example.com", "1.2.3.4");
350 mock_host_resolver->set_ondemand_mode(true);
351 auto state = mock_host_resolver->state();
352 builder_.set_host_resolver(std::move(mock_host_resolver));
353 std::unique_ptr<URLRequestContext> context(builder_.Build());
354
355 std::unique_ptr<HostResolver::ResolveHostRequest> request =
356 context->host_resolver()->CreateRequest(HostPortPair("example.com", 1234),
357 NetworkAnonymizationKey(),
358 NetLogWithSource(), std::nullopt);
359 TestCompletionCallback callback;
360 int rv = request->Start(callback.callback());
361 ASSERT_TRUE(state->has_pending_requests());
362
363 context.reset();
364
365 EXPECT_FALSE(state->has_pending_requests());
366
367 // Request should never complete.
368 base::RunLoop().RunUntilIdle();
369 EXPECT_THAT(rv, test::IsError(ERR_IO_PENDING));
370 EXPECT_FALSE(callback.have_result());
371 }
372
TEST_F(URLRequestContextBuilderTest,DefaultHostResolver)373 TEST_F(URLRequestContextBuilderTest, DefaultHostResolver) {
374 auto manager = std::make_unique<HostResolverManager>(
375 HostResolver::ManagerOptions(), nullptr /* system_dns_config_notifier */,
376 nullptr /* net_log */);
377
378 // Use a stack allocated builder instead of `builder_` to avoid dangling
379 // pointer of `manager`.
380 URLRequestContextBuilder builder;
381 SetUpURLRequestContextBuilder(builder);
382 builder.set_host_resolver_manager(manager.get());
383 std::unique_ptr<URLRequestContext> context = builder.Build();
384
385 EXPECT_EQ(context.get(), context->host_resolver()->GetContextForTesting());
386 EXPECT_EQ(manager.get(), context->host_resolver()->GetManagerForTesting());
387 }
388
TEST_F(URLRequestContextBuilderTest,CustomHostResolver)389 TEST_F(URLRequestContextBuilderTest, CustomHostResolver) {
390 std::unique_ptr<HostResolver> resolver =
391 HostResolver::CreateStandaloneResolver(nullptr);
392 ASSERT_FALSE(resolver->GetContextForTesting());
393
394 builder_.set_host_resolver(std::move(resolver));
395 std::unique_ptr<URLRequestContext> context = builder_.Build();
396
397 EXPECT_EQ(context.get(), context->host_resolver()->GetContextForTesting());
398 }
399
TEST_F(URLRequestContextBuilderTest,BindToNetworkFinalConfiguration)400 TEST_F(URLRequestContextBuilderTest, BindToNetworkFinalConfiguration) {
401 #if BUILDFLAG(IS_ANDROID)
402 if (base::android::BuildInfo::GetInstance()->sdk_int() <
403 base::android::SDK_VERSION_MARSHMALLOW) {
404 GTEST_SKIP()
405 << "BindToNetwork is supported starting from Android Marshmallow";
406 }
407
408 // The actual network handle doesn't really matter, this test just wants to
409 // check that all the pieces are in place and configured correctly.
410 constexpr handles::NetworkHandle network = 2;
411 auto scoped_mock_network_change_notifier =
412 std::make_unique<test::ScopedMockNetworkChangeNotifier>();
413 test::MockNetworkChangeNotifier* mock_ncn =
414 scoped_mock_network_change_notifier->mock_network_change_notifier();
415 mock_ncn->ForceNetworkHandlesSupported();
416
417 builder_.BindToNetwork(network);
418 std::unique_ptr<URLRequestContext> context = builder_.Build();
419
420 EXPECT_EQ(context->bound_network(), network);
421 EXPECT_EQ(context->host_resolver()->GetTargetNetworkForTesting(), network);
422 EXPECT_EQ(context->host_resolver()
423 ->GetManagerForTesting()
424 ->target_network_for_testing(),
425 network);
426 ASSERT_TRUE(context->GetNetworkSessionContext());
427 // A special factory that bind sockets to `network` is needed. We don't need
428 // to check exactly for that, the fact that we are not using the default one
429 // should be good enough.
430 EXPECT_NE(context->GetNetworkSessionContext()->client_socket_factory,
431 ClientSocketFactory::GetDefaultFactory());
432
433 const auto* quic_params = context->quic_context()->params();
434 EXPECT_FALSE(quic_params->close_sessions_on_ip_change);
435 EXPECT_FALSE(quic_params->goaway_sessions_on_ip_change);
436 EXPECT_FALSE(quic_params->migrate_sessions_on_network_change_v2);
437
438 const auto* network_session_params = context->GetNetworkSessionParams();
439 EXPECT_TRUE(network_session_params->ignore_ip_address_changes);
440 #else // !BUILDFLAG(IS_ANDROID)
441 GTEST_SKIP() << "BindToNetwork is supported only on Android";
442 #endif // BUILDFLAG(IS_ANDROID)
443 }
444
TEST_F(URLRequestContextBuilderTest,BindToNetworkCustomManagerOptions)445 TEST_F(URLRequestContextBuilderTest, BindToNetworkCustomManagerOptions) {
446 #if BUILDFLAG(IS_ANDROID)
447 if (base::android::BuildInfo::GetInstance()->sdk_int() <
448 base::android::SDK_VERSION_MARSHMALLOW) {
449 GTEST_SKIP()
450 << "BindToNetwork is supported starting from Android Marshmallow";
451 }
452
453 // The actual network handle doesn't really matter, this test just wants to
454 // check that all the pieces are in place and configured correctly.
455 constexpr handles::NetworkHandle network = 2;
456 auto scoped_mock_network_change_notifier =
457 std::make_unique<test::ScopedMockNetworkChangeNotifier>();
458 test::MockNetworkChangeNotifier* mock_ncn =
459 scoped_mock_network_change_notifier->mock_network_change_notifier();
460 mock_ncn->ForceNetworkHandlesSupported();
461
462 // Set non-default value for check_ipv6_on_wifi and check that this is what
463 // HostResolverManager receives.
464 HostResolver::ManagerOptions options;
465 options.check_ipv6_on_wifi = !options.check_ipv6_on_wifi;
466 builder_.BindToNetwork(network, options);
467 std::unique_ptr<URLRequestContext> context = builder_.Build();
468 EXPECT_EQ(context->host_resolver()
469 ->GetManagerForTesting()
470 ->check_ipv6_on_wifi_for_testing(),
471 options.check_ipv6_on_wifi);
472 #else // !BUILDFLAG(IS_ANDROID)
473 GTEST_SKIP() << "BindToNetwork is supported only on Android";
474 #endif // BUILDFLAG(IS_ANDROID)
475 }
476
TEST_F(URLRequestContextBuilderTest,MigrateSessionsOnNetworkChangeV2Default)477 TEST_F(URLRequestContextBuilderTest, MigrateSessionsOnNetworkChangeV2Default) {
478 std::unique_ptr<URLRequestContext> context = builder_.Build();
479
480 const QuicParams* quic_params = context->quic_context()->params();
481 #if BUILDFLAG(IS_ANDROID)
482 EXPECT_TRUE(quic_params->migrate_sessions_on_network_change_v2);
483 #else // !BUILDFLAG(IS_ANDROID)
484 EXPECT_FALSE(quic_params->migrate_sessions_on_network_change_v2);
485 #endif // BUILDFLAG(IS_ANDROID)
486 }
487
TEST_F(URLRequestContextBuilderTest,MigrateSessionsOnNetworkChangeV2Override)488 TEST_F(URLRequestContextBuilderTest, MigrateSessionsOnNetworkChangeV2Override) {
489 base::test::ScopedFeatureList scoped_list;
490 scoped_list.InitAndDisableFeature(
491 net::features::kMigrateSessionsOnNetworkChangeV2);
492 std::unique_ptr<URLRequestContext> context = builder_.Build();
493
494 const QuicParams* quic_params = context->quic_context()->params();
495 EXPECT_FALSE(quic_params->migrate_sessions_on_network_change_v2);
496 }
497
498 } // namespace
499
500 } // namespace net
501