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