OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
19 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
20 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
21 #include "net/log/test_net_log.h" | 21 #include "net/log/test_net_log.h" |
22 #include "net/log/test_net_log_entry.h" | 22 #include "net/log/test_net_log_entry.h" |
23 #include "net/log/test_net_log_util.h" | 23 #include "net/log/test_net_log_util.h" |
24 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 24 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
25 #include "net/proxy/mock_proxy_script_fetcher.h" | 25 #include "net/proxy/mock_proxy_script_fetcher.h" |
26 #include "net/proxy/proxy_config.h" | 26 #include "net/proxy/proxy_config.h" |
27 #include "net/proxy/proxy_resolver.h" | 27 #include "net/proxy/proxy_resolver.h" |
28 #include "net/proxy/proxy_script_decider.h" | 28 #include "net/proxy/proxy_script_decider.h" |
29 #include "net/proxy/proxy_script_fetcher.h" | 29 #include "net/proxy/proxy_script_fetcher.h" |
| 30 #include "net/test/gtest_util.h" |
30 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
32 | 34 |
| 35 using net::test::IsError; |
| 36 using net::test::IsOk; |
| 37 |
33 namespace net { | 38 namespace net { |
34 namespace { | 39 namespace { |
35 | 40 |
36 enum Error { | 41 enum Error { |
37 kFailedDownloading = -100, | 42 kFailedDownloading = -100, |
38 kFailedParsing = ERR_PAC_SCRIPT_FAILED, | 43 kFailedParsing = ERR_PAC_SCRIPT_FAILED, |
39 }; | 44 }; |
40 | 45 |
41 class Rules { | 46 class Rules { |
42 public: | 47 public: |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 | 344 |
340 private: | 345 private: |
341 URLRequestContext request_context_; | 346 URLRequestContext request_context_; |
342 }; | 347 }; |
343 | 348 |
344 // Fails if a synchronous DNS lookup success for wpad causes QuickCheck to fail. | 349 // Fails if a synchronous DNS lookup success for wpad causes QuickCheck to fail. |
345 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) { | 350 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) { |
346 resolver_.set_synchronous_mode(true); | 351 resolver_.set_synchronous_mode(true); |
347 resolver_.rules()->AddRule("wpad", "1.2.3.4"); | 352 resolver_.rules()->AddRule("wpad", "1.2.3.4"); |
348 | 353 |
349 EXPECT_EQ(OK, StartDecider()); | 354 EXPECT_THAT(StartDecider(), IsOk()); |
350 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); | 355 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); |
351 | 356 |
352 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 357 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
353 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); | 358 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); |
354 } | 359 } |
355 | 360 |
356 // Fails if an asynchronous DNS lookup success for wpad causes QuickCheck to | 361 // Fails if an asynchronous DNS lookup success for wpad causes QuickCheck to |
357 // fail. | 362 // fail. |
358 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncSuccess) { | 363 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncSuccess) { |
359 resolver_.set_ondemand_mode(true); | 364 resolver_.set_ondemand_mode(true); |
360 resolver_.rules()->AddRule("wpad", "1.2.3.4"); | 365 resolver_.rules()->AddRule("wpad", "1.2.3.4"); |
361 | 366 |
362 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 367 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
363 ASSERT_TRUE(resolver_.has_pending_requests()); | 368 ASSERT_TRUE(resolver_.has_pending_requests()); |
364 resolver_.ResolveAllPending(); | 369 resolver_.ResolveAllPending(); |
365 callback_.WaitForResult(); | 370 callback_.WaitForResult(); |
366 EXPECT_FALSE(resolver_.has_pending_requests()); | 371 EXPECT_FALSE(resolver_.has_pending_requests()); |
367 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); | 372 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); |
368 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 373 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
369 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); | 374 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); |
370 } | 375 } |
371 | 376 |
372 // Fails if an asynchronous DNS lookup failure (i.e. an NXDOMAIN) still causes | 377 // Fails if an asynchronous DNS lookup failure (i.e. an NXDOMAIN) still causes |
373 // ProxyScriptDecider to yield a PAC URL. | 378 // ProxyScriptDecider to yield a PAC URL. |
374 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncFail) { | 379 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncFail) { |
375 resolver_.set_ondemand_mode(true); | 380 resolver_.set_ondemand_mode(true); |
376 resolver_.rules()->AddSimulatedFailure("wpad"); | 381 resolver_.rules()->AddSimulatedFailure("wpad"); |
377 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 382 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
378 ASSERT_TRUE(resolver_.has_pending_requests()); | 383 ASSERT_TRUE(resolver_.has_pending_requests()); |
379 resolver_.ResolveAllPending(); | 384 resolver_.ResolveAllPending(); |
380 callback_.WaitForResult(); | 385 callback_.WaitForResult(); |
381 EXPECT_FALSE(decider_->effective_config().has_pac_url()); | 386 EXPECT_FALSE(decider_->effective_config().has_pac_url()); |
382 } | 387 } |
383 | 388 |
384 // Fails if a DNS lookup timeout either causes ProxyScriptDecider to yield a PAC | 389 // Fails if a DNS lookup timeout either causes ProxyScriptDecider to yield a PAC |
385 // URL or causes ProxyScriptDecider not to cancel its pending resolution. | 390 // URL or causes ProxyScriptDecider not to cancel its pending resolution. |
386 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncTimeout) { | 391 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncTimeout) { |
387 resolver_.set_ondemand_mode(true); | 392 resolver_.set_ondemand_mode(true); |
388 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 393 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
389 ASSERT_TRUE(resolver_.has_pending_requests()); | 394 ASSERT_TRUE(resolver_.has_pending_requests()); |
390 callback_.WaitForResult(); | 395 callback_.WaitForResult(); |
391 EXPECT_FALSE(resolver_.has_pending_requests()); | 396 EXPECT_FALSE(resolver_.has_pending_requests()); |
392 EXPECT_FALSE(decider_->effective_config().has_pac_url()); | 397 EXPECT_FALSE(decider_->effective_config().has_pac_url()); |
393 } | 398 } |
394 | 399 |
395 // Fails if DHCP check doesn't take place before QuickCheck. | 400 // Fails if DHCP check doesn't take place before QuickCheck. |
396 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckInhibitsDhcp) { | 401 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckInhibitsDhcp) { |
397 MockDhcpProxyScriptFetcher dhcp_fetcher; | 402 MockDhcpProxyScriptFetcher dhcp_fetcher; |
398 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; | 403 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; |
399 base::string16 pac_contents = base::UTF8ToUTF16(kPac); | 404 base::string16 pac_contents = base::UTF8ToUTF16(kPac); |
400 GURL url("http://foobar/baz"); | 405 GURL url("http://foobar/baz"); |
401 dhcp_fetcher.SetPacURL(url); | 406 dhcp_fetcher.SetPacURL(url); |
402 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher, NULL)); | 407 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher, NULL)); |
403 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 408 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
404 dhcp_fetcher.CompleteRequests(OK, pac_contents); | 409 dhcp_fetcher.CompleteRequests(OK, pac_contents); |
405 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 410 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
406 EXPECT_EQ(decider_->effective_config().pac_url(), url); | 411 EXPECT_EQ(decider_->effective_config().pac_url(), url); |
407 } | 412 } |
408 | 413 |
409 // Fails if QuickCheck still happens when disabled. To ensure QuickCheck is not | 414 // Fails if QuickCheck still happens when disabled. To ensure QuickCheck is not |
410 // happening, we add a synchronous failing resolver, which would ordinarily | 415 // happening, we add a synchronous failing resolver, which would ordinarily |
411 // mean a QuickCheck failure, then ensure that our ProxyScriptFetcher is still | 416 // mean a QuickCheck failure, then ensure that our ProxyScriptFetcher is still |
412 // asked to fetch. | 417 // asked to fetch. |
413 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckDisabled) { | 418 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckDisabled) { |
414 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; | 419 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; |
415 resolver_.set_synchronous_mode(true); | 420 resolver_.set_synchronous_mode(true); |
416 resolver_.rules()->AddSimulatedFailure("wpad"); | 421 resolver_.rules()->AddSimulatedFailure("wpad"); |
417 MockProxyScriptFetcher fetcher; | 422 MockProxyScriptFetcher fetcher; |
418 decider_.reset(new ProxyScriptDecider(&fetcher, &dhcp_fetcher_, NULL)); | 423 decider_.reset(new ProxyScriptDecider(&fetcher, &dhcp_fetcher_, NULL)); |
419 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 424 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
420 EXPECT_TRUE(fetcher.has_pending_request()); | 425 EXPECT_TRUE(fetcher.has_pending_request()); |
421 fetcher.NotifyFetchCompletion(OK, kPac); | 426 fetcher.NotifyFetchCompletion(OK, kPac); |
422 } | 427 } |
423 | 428 |
424 TEST_F(ProxyScriptDeciderQuickCheckTest, ExplicitPacUrl) { | 429 TEST_F(ProxyScriptDeciderQuickCheckTest, ExplicitPacUrl) { |
425 const char *kCustomUrl = "http://custom/proxy.pac"; | 430 const char *kCustomUrl = "http://custom/proxy.pac"; |
426 config_.set_pac_url(GURL(kCustomUrl)); | 431 config_.set_pac_url(GURL(kCustomUrl)); |
427 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl); | 432 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl); |
428 resolver_.rules()->AddSimulatedFailure("wpad"); | 433 resolver_.rules()->AddSimulatedFailure("wpad"); |
429 resolver_.rules()->AddRule("custom", "1.2.3.4"); | 434 resolver_.rules()->AddRule("custom", "1.2.3.4"); |
430 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 435 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
431 callback_.WaitForResult(); | 436 callback_.WaitForResult(); |
432 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 437 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
433 EXPECT_EQ(rule.url, decider_->effective_config().pac_url()); | 438 EXPECT_EQ(rule.url, decider_->effective_config().pac_url()); |
434 } | 439 } |
435 | 440 |
436 // Regression test for http://crbug.com/409698. | 441 // Regression test for http://crbug.com/409698. |
437 // This test lets the state machine get into state QUICK_CHECK_COMPLETE, then | 442 // This test lets the state machine get into state QUICK_CHECK_COMPLETE, then |
438 // destroys the decider, causing a cancel. | 443 // destroys the decider, causing a cancel. |
439 TEST_F(ProxyScriptDeciderQuickCheckTest, CancelPartway) { | 444 TEST_F(ProxyScriptDeciderQuickCheckTest, CancelPartway) { |
440 resolver_.set_synchronous_mode(false); | 445 resolver_.set_synchronous_mode(false); |
441 resolver_.set_ondemand_mode(true); | 446 resolver_.set_ondemand_mode(true); |
442 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 447 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); |
443 decider_.reset(NULL); | 448 decider_.reset(NULL); |
444 } | 449 } |
445 | 450 |
446 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 451 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
447 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { | 452 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { |
448 Rules rules; | 453 Rules rules; |
449 RuleBasedProxyScriptFetcher fetcher(&rules); | 454 RuleBasedProxyScriptFetcher fetcher(&rules); |
450 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 455 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
451 | 456 |
452 ProxyConfig config; | 457 ProxyConfig config; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 | 789 |
785 // Run the message loop to let the DHCP fetch complete and post the results | 790 // Run the message loop to let the DHCP fetch complete and post the results |
786 // back. Before the fix linked to above, this would try to invoke on | 791 // back. Before the fix linked to above, this would try to invoke on |
787 // the callback object provided by ProxyScriptDecider after it was | 792 // the callback object provided by ProxyScriptDecider after it was |
788 // no longer valid. | 793 // no longer valid. |
789 base::RunLoop().RunUntilIdle(); | 794 base::RunLoop().RunUntilIdle(); |
790 } | 795 } |
791 | 796 |
792 } // namespace | 797 } // namespace |
793 } // namespace net | 798 } // namespace net |
OLD | NEW |