• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.InitAndEnableFeature(
244       features::kPartitionConnectionsByNetworkIsolationKey);
245   const char kHost[] = "foo.test";
246 
247   ASSERT_TRUE(test_server_.Start());
248 
249   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
250 
251   GURL url(test_server_.GetURL(kHost, "/downloadable.pac"));
252   std::u16string text;
253   TestCompletionCallback callback;
254   int result = pac_fetcher->Fetch(url, &text, callback.callback(),
255                                   TRAFFIC_ANNOTATION_FOR_TESTS);
256   EXPECT_THAT(callback.GetResult(result), IsOk());
257   EXPECT_EQ(u"-downloadable.pac-\n", text);
258 
259   // Check that the URL in kDestination is in the HostCache, with
260   // the fetcher's IsolationInfo / NetworkAnonymizationKey, and no others.
261   net::HostResolver::ResolveHostParameters params;
262   params.source = net::HostResolverSource::LOCAL_ONLY;
263   std::unique_ptr<net::HostResolver::ResolveHostRequest> host_request =
264       context_->host_resolver()->CreateRequest(
265           url::SchemeHostPort(url),
266           pac_fetcher->isolation_info().network_anonymization_key(),
267           net::NetLogWithSource(), params);
268   net::TestCompletionCallback callback2;
269   result = host_request->Start(callback2.callback());
270   EXPECT_EQ(net::OK, callback2.GetResult(result));
271 
272   // Make sure there are no other entries in the HostCache (which would
273   // potentially be associated with other NetworkIsolationKeys).
274   EXPECT_EQ(1u, context_->host_resolver()->GetHostCache()->size());
275 
276   // Make sure the cache is actually returning different results based on
277   // NetworkAnonymizationKey.
278   host_request = context_->host_resolver()->CreateRequest(
279       url::SchemeHostPort(url), NetworkAnonymizationKey(),
280       net::NetLogWithSource(), params);
281   net::TestCompletionCallback callback3;
282   result = host_request->Start(callback3.callback());
283   EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, callback3.GetResult(result));
284 }
285 
286 // Verifies that PAC scripts are not being cached.
TEST_F(PacFileFetcherImplTest,NoCache)287 TEST_F(PacFileFetcherImplTest, NoCache) {
288   ASSERT_TRUE(test_server_.Start());
289 
290   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
291 
292   // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
293   GURL url(test_server_.GetURL("/cacheable_1hr.pac"));
294   {
295     std::u16string text;
296     TestCompletionCallback callback;
297     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
298                                     TRAFFIC_ANNOTATION_FOR_TESTS);
299     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
300     EXPECT_THAT(callback.WaitForResult(), IsOk());
301     EXPECT_EQ(u"-cacheable_1hr.pac-\n", text);
302   }
303 
304   // Kill the HTTP server.
305   ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete());
306 
307   // Try to fetch the file again. Since the server is not running anymore, the
308   // call should fail, thus indicating that the file was not fetched from the
309   // local cache.
310   {
311     std::u16string text;
312     TestCompletionCallback callback;
313     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
314                                     TRAFFIC_ANNOTATION_FOR_TESTS);
315     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
316 
317     // Expect any error. The exact error varies by platform.
318     EXPECT_NE(OK, callback.WaitForResult());
319   }
320 }
321 
TEST_F(PacFileFetcherImplTest,TooLarge)322 TEST_F(PacFileFetcherImplTest, TooLarge) {
323   ASSERT_TRUE(test_server_.Start());
324 
325   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
326 
327   {
328     // Set the maximum response size to 50 bytes.
329     int prev_size = pac_fetcher->SetSizeConstraint(50);
330 
331     // Try fetching URL that is 101 bytes large. We should abort the request
332     // after 50 bytes have been read, and fail with a too large error.
333     GURL url = test_server_.GetURL("/large-pac.nsproxy");
334     std::u16string text;
335     TestCompletionCallback callback;
336     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
337                                     TRAFFIC_ANNOTATION_FOR_TESTS);
338     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
339     EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FILE_TOO_BIG));
340     EXPECT_TRUE(text.empty());
341 
342     // Restore the original size bound.
343     pac_fetcher->SetSizeConstraint(prev_size);
344   }
345 
346   {
347     // Make sure we can still fetch regular URLs.
348     GURL url(test_server_.GetURL("/pac.nsproxy"));
349     std::u16string text;
350     TestCompletionCallback callback;
351     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
352                                     TRAFFIC_ANNOTATION_FOR_TESTS);
353     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
354     EXPECT_THAT(callback.WaitForResult(), IsOk());
355     EXPECT_EQ(u"-pac.nsproxy-\n", text);
356   }
357 }
358 
359 // The PacFileFetcher should be able to handle responses with an empty body.
TEST_F(PacFileFetcherImplTest,Empty)360 TEST_F(PacFileFetcherImplTest, Empty) {
361   ASSERT_TRUE(test_server_.Start());
362 
363   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
364 
365   GURL url(test_server_.GetURL("/empty"));
366   std::u16string text;
367   TestCompletionCallback callback;
368   int result = pac_fetcher->Fetch(url, &text, callback.callback(),
369                                   TRAFFIC_ANNOTATION_FOR_TESTS);
370   EXPECT_THAT(result, IsError(ERR_IO_PENDING));
371   EXPECT_THAT(callback.WaitForResult(), IsOk());
372   EXPECT_EQ(0u, text.size());
373 }
374 
TEST_F(PacFileFetcherImplTest,Hang)375 TEST_F(PacFileFetcherImplTest, Hang) {
376   ASSERT_TRUE(test_server_.Start());
377 
378   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
379 
380   // Set the timeout period to 0.5 seconds.
381   base::TimeDelta prev_timeout =
382       pac_fetcher->SetTimeoutConstraint(base::Milliseconds(500));
383 
384   // Try fetching a URL which takes 1.2 seconds. We should abort the request
385   // after 500 ms, and fail with a timeout error.
386   {
387     GURL url(test_server_.GetURL("/slow/proxy.pac?1.2"));
388     std::u16string text;
389     TestCompletionCallback callback;
390     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
391                                     TRAFFIC_ANNOTATION_FOR_TESTS);
392     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
393     EXPECT_THAT(callback.WaitForResult(), IsError(ERR_TIMED_OUT));
394     EXPECT_TRUE(text.empty());
395   }
396 
397   // Restore the original timeout period.
398   pac_fetcher->SetTimeoutConstraint(prev_timeout);
399 
400   {  // Make sure we can still fetch regular URLs.
401     GURL url(test_server_.GetURL("/pac.nsproxy"));
402     std::u16string text;
403     TestCompletionCallback callback;
404     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
405                                     TRAFFIC_ANNOTATION_FOR_TESTS);
406     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
407     EXPECT_THAT(callback.WaitForResult(), IsOk());
408     EXPECT_EQ(u"-pac.nsproxy-\n", text);
409   }
410 }
411 
412 // The PacFileFetcher should decode any content-codings
413 // (like gzip, bzip, etc.), and apply any charset conversions to yield
414 // UTF8.
TEST_F(PacFileFetcherImplTest,Encodings)415 TEST_F(PacFileFetcherImplTest, Encodings) {
416   ASSERT_TRUE(test_server_.Start());
417 
418   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
419 
420   // Test a response that is gzip-encoded -- should get inflated.
421   {
422     GURL url(test_server_.GetURL("/gzipped_pac"));
423     std::u16string text;
424     TestCompletionCallback callback;
425     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
426                                     TRAFFIC_ANNOTATION_FOR_TESTS);
427     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
428     EXPECT_THAT(callback.WaitForResult(), IsOk());
429     EXPECT_EQ(u"This data was gzipped.\n", text);
430   }
431 
432   // Test a response that was served as UTF-16 (BE). It should
433   // be converted to UTF8.
434   {
435     GURL url(test_server_.GetURL("/utf16be_pac"));
436     std::u16string text;
437     TestCompletionCallback callback;
438     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
439                                     TRAFFIC_ANNOTATION_FOR_TESTS);
440     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
441     EXPECT_THAT(callback.WaitForResult(), IsOk());
442     EXPECT_EQ(u"This was encoded as UTF-16BE.\n", text);
443   }
444 
445   // Test a response that lacks a charset, however starts with a UTF8 BOM.
446   {
447     GURL url(test_server_.GetURL("/utf8_bom"));
448     std::u16string text;
449     TestCompletionCallback callback;
450     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
451                                     TRAFFIC_ANNOTATION_FOR_TESTS);
452     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
453     EXPECT_THAT(callback.WaitForResult(), IsOk());
454     EXPECT_EQ(u"/* UTF8 */\n", text);
455   }
456 }
457 
TEST_F(PacFileFetcherImplTest,DataURLs)458 TEST_F(PacFileFetcherImplTest, DataURLs) {
459   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
460 
461   const char kEncodedUrl[] =
462       "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
463       "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
464       "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
465   const char16_t kPacScript[] =
466       u"function FindProxyForURL(url, host) {\n"
467       u"  if (host == 'foobar.com')\n"
468       u"    return 'PROXY blackhole:80';\n"
469       u"  return 'DIRECT';\n"
470       u"}";
471 
472   // Test fetching a "data:"-url containing a base64 encoded PAC script.
473   {
474     GURL url(kEncodedUrl);
475     std::u16string text;
476     TestCompletionCallback callback;
477     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
478                                     TRAFFIC_ANNOTATION_FOR_TESTS);
479     EXPECT_THAT(result, IsOk());
480     EXPECT_EQ(kPacScript, text);
481   }
482 
483   const char kEncodedUrlBroken[] =
484       "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
485 
486   // Test a broken "data:"-url containing a base64 encoded PAC script.
487   {
488     GURL url(kEncodedUrlBroken);
489     std::u16string text;
490     TestCompletionCallback callback;
491     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
492                                     TRAFFIC_ANNOTATION_FOR_TESTS);
493     EXPECT_THAT(result, IsError(ERR_FAILED));
494   }
495 }
496 
497 // Makes sure that a request gets through when the socket group for the PAC URL
498 // is full, so PacFileFetcherImpl can use the same URLRequestContext as
499 // everything else.
TEST_F(PacFileFetcherImplTest,IgnoresLimits)500 TEST_F(PacFileFetcherImplTest, IgnoresLimits) {
501   // Enough requests to exceed the per-group limit.
502   int num_requests = 2 + ClientSocketPoolManager::max_sockets_per_group(
503                              HttpNetworkSession::NORMAL_SOCKET_POOL);
504 
505   net::test_server::SimpleConnectionListener connection_listener(
506       num_requests, net::test_server::SimpleConnectionListener::
507                         FAIL_ON_ADDITIONAL_CONNECTIONS);
508   test_server_.SetConnectionListener(&connection_listener);
509   ASSERT_TRUE(test_server_.Start());
510 
511   std::u16string text;
512   TestCompletionCallback callback;
513   std::vector<std::unique_ptr<PacFileFetcherImpl>> pac_fetchers;
514   for (int i = 0; i < num_requests; i++) {
515     auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
516     GURL url(test_server_.GetURL("/hung"));
517     // Fine to use the same string and callback for all of these, as they should
518     // all hang.
519     int result = pac_fetcher->Fetch(url, &text, callback.callback(),
520                                     TRAFFIC_ANNOTATION_FOR_TESTS);
521     EXPECT_THAT(result, IsError(ERR_IO_PENDING));
522     pac_fetchers.push_back(std::move(pac_fetcher));
523   }
524 
525   connection_listener.WaitForConnections();
526   // None of the callbacks should have been invoked - all jobs should still be
527   // hung.
528   EXPECT_FALSE(callback.have_result());
529 
530   // Need to shut down the server before |connection_listener| is destroyed.
531   EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete());
532 }
533 
TEST_F(PacFileFetcherImplTest,OnShutdown)534 TEST_F(PacFileFetcherImplTest, OnShutdown) {
535   ASSERT_TRUE(test_server_.Start());
536 
537   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
538   std::u16string text;
539   TestCompletionCallback callback;
540   int result =
541       pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
542                          callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
543   EXPECT_THAT(result, IsError(ERR_IO_PENDING));
544   EXPECT_EQ(1u, context_->url_requests()->size());
545 
546   pac_fetcher->OnShutdown();
547   EXPECT_EQ(0u, context_->url_requests()->size());
548   EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONTEXT_SHUT_DOWN));
549 
550   // Make sure there's no asynchronous completion notification.
551   base::RunLoop().RunUntilIdle();
552   EXPECT_EQ(0u, context_->url_requests()->size());
553   EXPECT_FALSE(callback.have_result());
554 
555   result =
556       pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
557                          callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
558   EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN));
559 }
560 
TEST_F(PacFileFetcherImplTest,OnShutdownWithNoLiveRequest)561 TEST_F(PacFileFetcherImplTest, OnShutdownWithNoLiveRequest) {
562   ASSERT_TRUE(test_server_.Start());
563 
564   auto pac_fetcher = PacFileFetcherImpl::Create(context_.get());
565   pac_fetcher->OnShutdown();
566 
567   std::u16string text;
568   TestCompletionCallback callback;
569   int result =
570       pac_fetcher->Fetch(test_server_.GetURL("/hung"), &text,
571                          callback.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
572   EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN));
573   EXPECT_EQ(0u, context_->url_requests()->size());
574 }
575 
576 }  // namespace
577 
578 }  // namespace net
579