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/proxy_resolution/pac_file_fetcher_impl.h"
6
7 #include <optional>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/path_service.h"
16 #include "base/run_loop.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/task/sequenced_task_runner.h"
19 #include "base/task/single_thread_task_runner.h"
20 #include "base/test/scoped_feature_list.h"
21 #include "base/test/task_environment.h"
22 #include "net/base/features.h"
23 #include "net/base/filename_util.h"
24 #include "net/base/load_flags.h"
25 #include "net/base/network_delegate_impl.h"
26 #include "net/base/test_completion_callback.h"
27 #include "net/cert/mock_cert_verifier.h"
28 #include "net/cert/multi_log_ct_verifier.h"
29 #include "net/disk_cache/disk_cache.h"
30 #include "net/dns/mock_host_resolver.h"
31 #include "net/http/http_cache.h"
32 #include "net/http/http_network_session.h"
33 #include "net/http/http_server_properties.h"
34 #include "net/http/http_transaction_factory.h"
35 #include "net/http/transport_security_state.h"
36 #include "net/net_buildflags.h"
37 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
38 #include "net/quic/quic_context.h"
39 #include "net/socket/client_socket_pool_manager.h"
40 #include "net/socket/transport_client_socket_pool.h"
41 #include "net/ssl/ssl_config_service_defaults.h"
42 #include "net/test/embedded_test_server/embedded_test_server.h"
43 #include "net/test/embedded_test_server/simple_connection_listener.h"
44 #include "net/test/gtest_util.h"
45 #include "net/test/test_with_task_environment.h"
46 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
47 #include "net/url_request/url_request_context_builder.h"
48 #include "net/url_request/url_request_job_factory.h"
49 #include "net/url_request/url_request_test_util.h"
50 #include "testing/gmock/include/gmock/gmock.h"
51 #include "testing/gtest/include/gtest/gtest.h"
52 #include "testing/platform_test.h"
53
54 using net::test::IsError;
55 using net::test::IsOk;
56
57 using base::ASCIIToUTF16;
58
59 // TODO(eroman):
60 // - Test canceling an outstanding request.
61 // - Test deleting PacFileFetcher while a request is in progress.
62
63 namespace net {
64
65 namespace {
66
67 const base::FilePath::CharType kDocRoot[] =
68 FILE_PATH_LITERAL("net/data/pac_file_fetcher_unittest");
69
70 struct FetchResult {
71 int code;
72 std::u16string text;
73 };
74
75 // Get a file:// url relative to net/data/proxy/pac_file_fetcher_unittest.
GetTestFileUrl(const std::string & relpath)76 GURL GetTestFileUrl(const std::string& relpath) {
77 base::FilePath path;
78 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
79 path = path.AppendASCII("net");
80 path = path.AppendASCII("data");
81 path = path.AppendASCII("pac_file_fetcher_unittest");
82 GURL base_url = FilePathToFileURL(path);
83 return GURL(base_url.spec() + "/" + relpath);
84 }
85
86 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
87 // without introducing layering violations. Also causes a test failure if a
88 // request is seen that doesn't set a load flag to bypass revocation checking.
89
90 class BasicNetworkDelegate : public NetworkDelegateImpl {
91 public:
92 BasicNetworkDelegate() = default;
93
94 BasicNetworkDelegate(const BasicNetworkDelegate&) = delete;
95 BasicNetworkDelegate& operator=(const BasicNetworkDelegate&) = delete;
96
97 ~BasicNetworkDelegate() override = default;
98
99 private:
OnBeforeURLRequest(URLRequest * request,CompletionOnceCallback callback,GURL * new_url)100 int OnBeforeURLRequest(URLRequest* request,
101 CompletionOnceCallback callback,
102 GURL* new_url) override {
103 EXPECT_TRUE(request->load_flags() & LOAD_DISABLE_CERT_NETWORK_FETCHES);
104 return OK;
105 }
106 };
107
108 class PacFileFetcherImplTest : public PlatformTest, public WithTaskEnvironment {
109 public:
PacFileFetcherImplTest()110 PacFileFetcherImplTest() {
111 test_server_.AddDefaultHandlers(base::FilePath(kDocRoot));
112 auto builder = CreateTestURLRequestContextBuilder();
113 network_delegate_ =
114 builder->set_network_delegate(std::make_unique<BasicNetworkDelegate>());
115 context_ = builder->Build();
116 }
117
118 protected:
119 EmbeddedTestServer test_server_;
120 std::unique_ptr<URLRequestContext> context_;
121 // Owned by `context_`.
122 raw_ptr<BasicNetworkDelegate> network_delegate_;
123 };
124
TEST_F(PacFileFetcherImplTest,FileUrlNotAllowed)125 TEST_F(PacFileFetcherImplTest, FileUrlNotAllowed) {
126 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
127
128 // Fetch a file that exists, however the PacFileFetcherImpl does not allow use
129 // of file://.
130 std::u16string text;
131 TestCompletionCallback callback;
132 int result =
133 pac_fetcher->Fetch(GetTestFileUrl("pac.txt"), &text, callback.callback(),
134 TRAFFIC_ANNOTATION_FOR_TESTS);
135 EXPECT_THAT(result, IsError(ERR_DISALLOWED_URL_SCHEME));
136 }
137
138 // Redirect to file URLs are not allowed.
TEST_F(PacFileFetcherImplTest,RedirectToFileUrl)139 TEST_F(PacFileFetcherImplTest, RedirectToFileUrl) {
140 ASSERT_TRUE(test_server_.Start());
141
142 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
143
144 GURL url(test_server_.GetURL("/redirect-to-file"));
145
146 std::u16string text;
147 TestCompletionCallback callback;
148 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
149 TRAFFIC_ANNOTATION_FOR_TESTS);
150 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
151 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_UNSAFE_REDIRECT));
152 }
153
154 // Note that all mime types are allowed for PAC file, to be consistent
155 // with other browsers.
TEST_F(PacFileFetcherImplTest,HttpMimeType)156 TEST_F(PacFileFetcherImplTest, HttpMimeType) {
157 ASSERT_TRUE(test_server_.Start());
158
159 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
160
161 { // Fetch a PAC with mime type "text/plain"
162 GURL url(test_server_.GetURL("/pac.txt"));
163 std::u16string text;
164 TestCompletionCallback callback;
165 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
166 TRAFFIC_ANNOTATION_FOR_TESTS);
167 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
168 EXPECT_THAT(callback.WaitForResult(), IsOk());
169 EXPECT_EQ(u"-pac.txt-\n", text);
170 }
171 { // Fetch a PAC with mime type "text/html"
172 GURL url(test_server_.GetURL("/pac.html"));
173 std::u16string text;
174 TestCompletionCallback callback;
175 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
176 TRAFFIC_ANNOTATION_FOR_TESTS);
177 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
178 EXPECT_THAT(callback.WaitForResult(), IsOk());
179 EXPECT_EQ(u"-pac.html-\n", text);
180 }
181 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
182 GURL url(test_server_.GetURL("/pac.nsproxy"));
183 std::u16string text;
184 TestCompletionCallback callback;
185 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
186 TRAFFIC_ANNOTATION_FOR_TESTS);
187 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
188 EXPECT_THAT(callback.WaitForResult(), IsOk());
189 EXPECT_EQ(u"-pac.nsproxy-\n", text);
190 }
191 }
192
TEST_F(PacFileFetcherImplTest,HttpStatusCode)193 TEST_F(PacFileFetcherImplTest, HttpStatusCode) {
194 ASSERT_TRUE(test_server_.Start());
195
196 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
197
198 { // Fetch a PAC which gives a 500 -- FAIL
199 GURL url(test_server_.GetURL("/500.pac"));
200 std::u16string text;
201 TestCompletionCallback callback;
202 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
203 TRAFFIC_ANNOTATION_FOR_TESTS);
204 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
205 EXPECT_THAT(callback.WaitForResult(),
206 IsError(ERR_HTTP_RESPONSE_CODE_FAILURE));
207 EXPECT_TRUE(text.empty());
208 }
209 { // Fetch a PAC which gives a 404 -- FAIL
210 GURL url(test_server_.GetURL("/404.pac"));
211 std::u16string text;
212 TestCompletionCallback callback;
213 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
214 TRAFFIC_ANNOTATION_FOR_TESTS);
215 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
216 EXPECT_THAT(callback.WaitForResult(),
217 IsError(ERR_HTTP_RESPONSE_CODE_FAILURE));
218 EXPECT_TRUE(text.empty());
219 }
220 }
221
TEST_F(PacFileFetcherImplTest,ContentDisposition)222 TEST_F(PacFileFetcherImplTest, ContentDisposition) {
223 ASSERT_TRUE(test_server_.Start());
224
225 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
226
227 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
228 // have no effect.
229 GURL url(test_server_.GetURL("/downloadable.pac"));
230 std::u16string text;
231 TestCompletionCallback callback;
232 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
233 TRAFFIC_ANNOTATION_FOR_TESTS);
234 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
235 EXPECT_THAT(callback.WaitForResult(), IsOk());
236 EXPECT_EQ(u"-downloadable.pac-\n", text);
237 }
238
239 // Verifies that fetches are made using the fetcher's IsolationInfo, by checking
240 // the DNS cache.
TEST_F(PacFileFetcherImplTest,IsolationInfo)241 TEST_F(PacFileFetcherImplTest, IsolationInfo) {
242 base::test::ScopedFeatureList feature_list;
243 feature_list.InitWithFeatures(
244 // enabled_features
245 {features::kPartitionConnectionsByNetworkIsolationKey,
246 features::kSplitHostCacheByNetworkIsolationKey},
247 // disabled_features
248 {});
249 const char kHost[] = "foo.test";
250
251 ASSERT_TRUE(test_server_.Start());
252
253 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
254
255 GURL url(test_server_.GetURL(kHost, "/downloadable.pac"));
256 std::u16string text;
257 TestCompletionCallback callback;
258 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
259 TRAFFIC_ANNOTATION_FOR_TESTS);
260 EXPECT_THAT(callback.GetResult(result), IsOk());
261 EXPECT_EQ(u"-downloadable.pac-\n", text);
262
263 // Check that the URL in kDestination is in the HostCache, with
264 // the fetcher's IsolationInfo / NetworkAnonymizationKey, and no others.
265 net::HostResolver::ResolveHostParameters params;
266 params.source = net::HostResolverSource::LOCAL_ONLY;
267 std::unique_ptr<net::HostResolver::ResolveHostRequest> host_request =
268 context_->host_resolver()->CreateRequest(
269 url::SchemeHostPort(url),
270 pac_fetcher->isolation_info().network_anonymization_key(),
271 net::NetLogWithSource(), params);
272 net::TestCompletionCallback callback2;
273 result = host_request->Start(callback2.callback());
274 EXPECT_EQ(net::OK, callback2.GetResult(result));
275
276 // Make sure there are no other entries in the HostCache (which would
277 // potentially be associated with other NetworkIsolationKeys).
278 EXPECT_EQ(1u, context_->host_resolver()->GetHostCache()->size());
279
280 // Make sure the cache is actually returning different results based on
281 // NetworkAnonymizationKey.
282 host_request = context_->host_resolver()->CreateRequest(
283 url::SchemeHostPort(url), NetworkAnonymizationKey(),
284 net::NetLogWithSource(), params);
285 net::TestCompletionCallback callback3;
286 result = host_request->Start(callback3.callback());
287 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, callback3.GetResult(result));
288 }
289
290 // Verifies that PAC scripts are not being cached.
TEST_F(PacFileFetcherImplTest,NoCache)291 TEST_F(PacFileFetcherImplTest, NoCache) {
292 ASSERT_TRUE(test_server_.Start());
293
294 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
295
296 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
297 GURL url(test_server_.GetURL("/cacheable_1hr.pac"));
298 {
299 std::u16string text;
300 TestCompletionCallback callback;
301 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
302 TRAFFIC_ANNOTATION_FOR_TESTS);
303 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
304 EXPECT_THAT(callback.WaitForResult(), IsOk());
305 EXPECT_EQ(u"-cacheable_1hr.pac-\n", text);
306 }
307
308 // Kill the HTTP server.
309 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete());
310
311 // Try to fetch the file again. Since the server is not running anymore, the
312 // call should fail, thus indicating that the file was not fetched from the
313 // local cache.
314 {
315 std::u16string text;
316 TestCompletionCallback callback;
317 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
318 TRAFFIC_ANNOTATION_FOR_TESTS);
319 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
320
321 // Expect any error. The exact error varies by platform.
322 EXPECT_NE(OK, callback.WaitForResult());
323 }
324 }
325
TEST_F(PacFileFetcherImplTest,TooLarge)326 TEST_F(PacFileFetcherImplTest, TooLarge) {
327 ASSERT_TRUE(test_server_.Start());
328
329 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
330
331 {
332 // Set the maximum response size to 50 bytes.
333 int prev_size = pac_fetcher->SetSizeConstraint(50);
334
335 // Try fetching URL that is 101 bytes large. We should abort the request
336 // after 50 bytes have been read, and fail with a too large error.
337 GURL url = test_server_.GetURL("/large-pac.nsproxy");
338 std::u16string text;
339 TestCompletionCallback callback;
340 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
341 TRAFFIC_ANNOTATION_FOR_TESTS);
342 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
343 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FILE_TOO_BIG));
344 EXPECT_TRUE(text.empty());
345
346 // Restore the original size bound.
347 pac_fetcher->SetSizeConstraint(prev_size);
348 }
349
350 {
351 // Make sure we can still fetch regular URLs.
352 GURL url(test_server_.GetURL("/pac.nsproxy"));
353 std::u16string text;
354 TestCompletionCallback callback;
355 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
356 TRAFFIC_ANNOTATION_FOR_TESTS);
357 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
358 EXPECT_THAT(callback.WaitForResult(), IsOk());
359 EXPECT_EQ(u"-pac.nsproxy-\n", text);
360 }
361 }
362
363 // The PacFileFetcher should be able to handle responses with an empty body.
TEST_F(PacFileFetcherImplTest,Empty)364 TEST_F(PacFileFetcherImplTest, Empty) {
365 ASSERT_TRUE(test_server_.Start());
366
367 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
368
369 GURL url(test_server_.GetURL("/empty"));
370 std::u16string text;
371 TestCompletionCallback callback;
372 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
373 TRAFFIC_ANNOTATION_FOR_TESTS);
374 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
375 EXPECT_THAT(callback.WaitForResult(), IsOk());
376 EXPECT_EQ(0u, text.size());
377 }
378
TEST_F(PacFileFetcherImplTest,Hang)379 TEST_F(PacFileFetcherImplTest, Hang) {
380 ASSERT_TRUE(test_server_.Start());
381
382 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
383
384 // Set the timeout period to 0.5 seconds.
385 base::TimeDelta prev_timeout =
386 pac_fetcher->SetTimeoutConstraint(base::Milliseconds(500));
387
388 // Try fetching a URL which takes 1.2 seconds. We should abort the request
389 // after 500 ms, and fail with a timeout error.
390 {
391 GURL url(test_server_.GetURL("/slow/proxy.pac?1.2"));
392 std::u16string text;
393 TestCompletionCallback callback;
394 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
395 TRAFFIC_ANNOTATION_FOR_TESTS);
396 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
397 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_TIMED_OUT));
398 EXPECT_TRUE(text.empty());
399 }
400
401 // Restore the original timeout period.
402 pac_fetcher->SetTimeoutConstraint(prev_timeout);
403
404 { // Make sure we can still fetch regular URLs.
405 GURL url(test_server_.GetURL("/pac.nsproxy"));
406 std::u16string text;
407 TestCompletionCallback callback;
408 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
409 TRAFFIC_ANNOTATION_FOR_TESTS);
410 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
411 EXPECT_THAT(callback.WaitForResult(), IsOk());
412 EXPECT_EQ(u"-pac.nsproxy-\n", text);
413 }
414 }
415
416 // The PacFileFetcher should decode any content-codings
417 // (like gzip, bzip, etc.), and apply any charset conversions to yield
418 // UTF8.
TEST_F(PacFileFetcherImplTest,Encodings)419 TEST_F(PacFileFetcherImplTest, Encodings) {
420 ASSERT_TRUE(test_server_.Start());
421
422 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
423
424 // Test a response that is gzip-encoded -- should get inflated.
425 {
426 GURL url(test_server_.GetURL("/gzipped_pac"));
427 std::u16string text;
428 TestCompletionCallback callback;
429 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
430 TRAFFIC_ANNOTATION_FOR_TESTS);
431 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
432 EXPECT_THAT(callback.WaitForResult(), IsOk());
433 EXPECT_EQ(u"This data was gzipped.\n", text);
434 }
435
436 // Test a response that was served as UTF-16 (BE). It should
437 // be converted to UTF8.
438 {
439 GURL url(test_server_.GetURL("/utf16be_pac"));
440 std::u16string text;
441 TestCompletionCallback callback;
442 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
443 TRAFFIC_ANNOTATION_FOR_TESTS);
444 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
445 EXPECT_THAT(callback.WaitForResult(), IsOk());
446 EXPECT_EQ(u"This was encoded as UTF-16BE.\n", text);
447 }
448
449 // Test a response that lacks a charset, however starts with a UTF8 BOM.
450 {
451 GURL url(test_server_.GetURL("/utf8_bom"));
452 std::u16string text;
453 TestCompletionCallback callback;
454 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
455 TRAFFIC_ANNOTATION_FOR_TESTS);
456 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
457 EXPECT_THAT(callback.WaitForResult(), IsOk());
458 EXPECT_EQ(u"/* UTF8 */\n", text);
459 }
460 }
461
TEST_F(PacFileFetcherImplTest,DataURLs)462 TEST_F(PacFileFetcherImplTest, DataURLs) {
463 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
464
465 const char kEncodedUrl[] =
466 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
467 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
468 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
469 const char16_t kPacScript[] =
470 u"function FindProxyForURL(url, host) {\n"
471 u" if (host == 'foobar.com')\n"
472 u" return 'PROXY blackhole:80';\n"
473 u" return 'DIRECT';\n"
474 u"}";
475
476 // Test fetching a "data:"-url containing a base64 encoded PAC script.
477 {
478 GURL url(kEncodedUrl);
479 std::u16string text;
480 TestCompletionCallback callback;
481 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
482 TRAFFIC_ANNOTATION_FOR_TESTS);
483 EXPECT_THAT(result, IsOk());
484 EXPECT_EQ(kPacScript, text);
485 }
486
487 const char kEncodedUrlBroken[] =
488 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
489
490 // Test a broken "data:"-url containing a base64 encoded PAC script.
491 {
492 GURL url(kEncodedUrlBroken);
493 std::u16string text;
494 TestCompletionCallback callback;
495 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
496 TRAFFIC_ANNOTATION_FOR_TESTS);
497 EXPECT_THAT(result, IsError(ERR_FAILED));
498 }
499 }
500
501 // Makes sure that a request gets through when the socket group for the PAC URL
502 // is full, so PacFileFetcherImpl can use the same URLRequestContext as
503 // everything else.
TEST_F(PacFileFetcherImplTest,IgnoresLimits)504 TEST_F(PacFileFetcherImplTest, IgnoresLimits) {
505 // Enough requests to exceed the per-group limit.
506 int num_requests = 2 + ClientSocketPoolManager::max_sockets_per_group(
507 HttpNetworkSession::NORMAL_SOCKET_POOL);
508
509 net::test_server::SimpleConnectionListener connection_listener(
510 num_requests, net::test_server::SimpleConnectionListener::
511 FAIL_ON_ADDITIONAL_CONNECTIONS);
512 test_server_.SetConnectionListener(&connection_listener);
513 ASSERT_TRUE(test_server_.Start());
514
515 std::u16string text;
516 TestCompletionCallback callback;
517 std::vector<std::unique_ptr<PacFileFetcherImpl>> pac_fetchers;
518 for (int i = 0; i < num_requests; i++) {
519 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
520 GURL url(test_server_.GetURL("/hung"));
521 // Fine to use the same string and callback for all of these, as they should
522 // all hang.
523 int result = pac_fetcher->Fetch(url, &text, callback.callback(),
524 TRAFFIC_ANNOTATION_FOR_TESTS);
525 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
526 pac_fetchers.push_back(std::move(pac_fetcher));
527 }
528
529 connection_listener.WaitForConnections();
530 // None of the callbacks should have been invoked - all jobs should still be
531 // hung.
532 EXPECT_FALSE(callback.have_result());
533
534 // Need to shut down the server before |connection_listener| is destroyed.
535 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete());
536 }
537
TEST_F(PacFileFetcherImplTest,OnShutdown)538 TEST_F(PacFileFetcherImplTest, OnShutdown) {
539 ASSERT_TRUE(test_server_.Start());
540
541 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
542 std::u16string text;
543 TestCompletionCallback callback;
544 int result =
545 pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
546 callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
547 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
548 EXPECT_EQ(1u, context_->url_requests()->size());
549
550 pac_fetcher->OnShutdown();
551 EXPECT_EQ(0u, context_->url_requests()->size());
552 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONTEXT_SHUT_DOWN));
553
554 // Make sure there's no asynchronous completion notification.
555 base::RunLoop().RunUntilIdle();
556 EXPECT_EQ(0u, context_->url_requests()->size());
557 EXPECT_FALSE(callback.have_result());
558
559 result =
560 pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
561 callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
562 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN));
563 }
564
TEST_F(PacFileFetcherImplTest,OnShutdownWithNoLiveRequest)565 TEST_F(PacFileFetcherImplTest, OnShutdownWithNoLiveRequest) {
566 ASSERT_TRUE(test_server_.Start());
567
568 auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
569 pac_fetcher->OnShutdown();
570
571 std::u16string text;
572 TestCompletionCallback callback;
573 int result =
574 pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
575 callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
576 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN));
577 EXPECT_EQ(0u, context_->url_requests()->size());
578 }
579
580 } // namespace
581
582 } // namespace net
583