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 "net/proxy/proxy_service.h" | 5 #include "net/proxy/proxy_service.h" |
6 | 6 |
7 #include <cstdarg> | 7 #include <cstdarg> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 3462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3473 bool synchronous_success = service.TryResolveProxySynchronously( | 3473 bool synchronous_success = service.TryResolveProxySynchronously( |
3474 url, std::string(), LOAD_NORMAL, &info, nullptr, log.bound()); | 3474 url, std::string(), LOAD_NORMAL, &info, nullptr, log.bound()); |
3475 EXPECT_TRUE(synchronous_success); | 3475 EXPECT_TRUE(synchronous_success); |
3476 EXPECT_FALSE(info.is_direct()); | 3476 EXPECT_FALSE(info.is_direct()); |
3477 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); | 3477 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); |
3478 | 3478 |
3479 // No request should have been queued. | 3479 // No request should have been queued. |
3480 EXPECT_EQ(0u, factory->pending_requests().size()); | 3480 EXPECT_EQ(0u, factory->pending_requests().size()); |
3481 } | 3481 } |
3482 | 3482 |
3483 // This test checks that URLs are sanitized before being passed on to the proxy | |
3484 // resolver (i.e. PAC scripts). | |
3485 TEST_F(ProxyServiceTest, SanitizeUrlsBeforeResolving) { | |
mmenke
2016/05/20 17:40:23
Didn't realize you had this sort of test as well,
eroman
2016/05/20 21:23:53
Done.
I agree that is a better approach.
All of th
| |
3486 MockProxyConfigService* config_service = | |
3487 new MockProxyConfigService("http://foopy/proxy.pac"); | |
3488 MockAsyncProxyResolver resolver; | |
3489 MockAsyncProxyResolverFactory* factory = | |
3490 new MockAsyncProxyResolverFactory(false); | |
3491 | |
3492 ProxyService service(base::WrapUnique(config_service), | |
3493 base::WrapUnique(factory), nullptr); | |
3494 | |
3495 // Request 1 ---------------------------------------------------------------- | |
3496 | |
3497 // This URL includes a lot of different parts (some of these are expected to | |
3498 // be stripped). | |
3499 GURL url1("http://username:password@www.google.com:8080/path?query#fragment"); | |
3500 GURL sanitized_url1("http://www.google.com:8080/path?query"); | |
3501 | |
3502 ProxyInfo info1; | |
3503 TestCompletionCallback callback1; | |
3504 int rv = service.ResolveProxy(url1, std::string(), LOAD_NORMAL, &info1, | |
3505 callback1.callback(), nullptr, nullptr, | |
3506 BoundNetLog()); | |
3507 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3508 | |
3509 // First step is to download the PAC script. | |
3510 EXPECT_EQ(GURL("http://foopy/proxy.pac"), | |
3511 factory->pending_requests()[0]->script_data()->url()); | |
3512 factory->pending_requests()[0]->CompleteNowWithForwarder(OK, &resolver); | |
3513 | |
3514 ASSERT_EQ(1u, resolver.pending_requests().size()); | |
3515 // Ensure that the sanitized URL was sent to the proxy resolver (not the full | |
3516 // URL). | |
3517 EXPECT_EQ(sanitized_url1, resolver.pending_requests()[0]->url()); | |
3518 | |
3519 // Complete the request. | |
3520 resolver.pending_requests()[0]->results()->UsePacString("DIRECT"); | |
3521 resolver.pending_requests()[0]->CompleteNow(OK); | |
3522 EXPECT_EQ(OK, callback1.WaitForResult()); | |
3523 EXPECT_TRUE(info1.is_direct()); | |
3524 | |
3525 // Request 2 ---------------------------------------------------------------- | |
3526 | |
3527 // Start another request. This time the URL is https://, so a different | |
3528 // sanitization strategy should be used. | |
3529 GURL url2( | |
3530 "https://username:password@www.google.com:8080/path?query#fragment"); | |
3531 GURL sanitized_url2("https://www.google.com:8080/"); | |
3532 | |
3533 ProxyInfo info2; | |
3534 TestCompletionCallback callback2; | |
3535 rv = service.ResolveProxy(url2, std::string(), LOAD_NORMAL, &info2, | |
3536 callback2.callback(), nullptr, nullptr, | |
3537 BoundNetLog()); | |
3538 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3539 | |
3540 ASSERT_EQ(1u, resolver.pending_requests().size()); | |
3541 // Ensure that the sanitized URL was sent to the proxy resolver (not the full | |
3542 // URL). | |
3543 EXPECT_EQ(sanitized_url2, resolver.pending_requests()[0]->url()); | |
3544 | |
3545 // Complete the request. | |
3546 resolver.pending_requests()[0]->results()->UsePacString("DIRECT"); | |
3547 resolver.pending_requests()[0]->CompleteNow(OK); | |
3548 EXPECT_EQ(OK, callback2.WaitForResult()); | |
3549 EXPECT_TRUE(info2.is_direct()); | |
3550 | |
3551 // Request 3 ---------------------------------------------------------------- | |
3552 | |
3553 // Change the sanitization policy. | |
3554 service.set_sanitize_url_policy(ProxyService::SanitizeUrlPolicy::UNSAFE); | |
3555 | |
3556 // Start another request. This is the same as request 2, however the expected | |
3557 // sanitized URL is different (includes the path and query). | |
3558 GURL url3 = url2; | |
3559 GURL sanitized_url3("https://www.google.com:8080/path?query"); | |
3560 | |
3561 ProxyInfo info3; | |
3562 TestCompletionCallback callback3; | |
3563 rv = service.ResolveProxy(url3, std::string(), LOAD_NORMAL, &info3, | |
3564 callback3.callback(), nullptr, nullptr, | |
3565 BoundNetLog()); | |
3566 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3567 | |
3568 ASSERT_EQ(1u, resolver.pending_requests().size()); | |
3569 // Ensure that the sanitized URL was sent to the proxy resolver (not the full | |
3570 // URL). | |
3571 EXPECT_EQ(sanitized_url3, resolver.pending_requests()[0]->url()); | |
3572 | |
3573 // Complete the request. | |
3574 resolver.pending_requests()[0]->results()->UsePacString("DIRECT"); | |
3575 resolver.pending_requests()[0]->CompleteNow(OK); | |
3576 EXPECT_EQ(OK, callback3.WaitForResult()); | |
3577 EXPECT_TRUE(info3.is_direct()); | |
3578 } | |
3579 | |
3580 // Tests SanitizeUrlForPacScript() using input URLs that have a | |
3581 // non-cryptographic scheme (i.e. http://). The sanitized result is consistent | |
3582 // regardless of the stripping mode selected. | |
3583 TEST_F(ProxyServiceTest, SanitizeUrlForPacScriptNonCryptographic) { | |
3584 const struct { | |
3585 const char* raw_url; | |
3586 const char* sanitized_url; | |
3587 } kTests[] = { | |
3588 // Embedded identity is stripped. | |
3589 { | |
3590 "http://foo:bar@example.com/", "http://example.com/", | |
3591 }, | |
3592 { | |
3593 "ftp://foo:bar@example.com/", "ftp://example.com/", | |
mmenke
2016/05/20 17:40:23
Seems like we should have a test with an FTP URL w
eroman
2016/05/20 21:23:53
Done.
| |
3594 }, | |
3595 // Reference fragment is stripped. | |
3596 { | |
3597 "http://example.com/blah#hello", "http://example.com/blah", | |
3598 }, | |
3599 // Query parameters are NOT stripped. | |
3600 { | |
3601 "http://example.com/foo/bar/baz?hello", | |
3602 "http://example.com/foo/bar/baz?hello", | |
3603 }, | |
3604 // Fragment is stripped, but path and query are left intact. | |
3605 { | |
3606 "http://foo:bar@example.com/foo/bar/baz?hello#sigh", | |
3607 "http://example.com/foo/bar/baz?hello", | |
3608 }, | |
3609 // Port numbers are not affected. | |
3610 { | |
3611 "http://example.com:88/hi", "http://example.com:88/hi", | |
3612 }, | |
3613 }; | |
3614 | |
3615 for (const auto& test : kTests) { | |
3616 // The result of SanitizeUrlForPacScript() is the same regardless of the | |
3617 // second parameter (sanitization mode), since the input URLs do not use a | |
3618 // cryptographic scheme. | |
3619 GURL raw_url(test.raw_url); | |
3620 ASSERT_TRUE(raw_url.is_valid()); | |
3621 EXPECT_FALSE(raw_url.SchemeIsCryptographic()); | |
3622 | |
3623 EXPECT_EQ(GURL(test.sanitized_url), | |
3624 ProxyService::SanitizeUrl( | |
3625 raw_url, ProxyService::SanitizeUrlPolicy::UNSAFE)); | |
3626 | |
3627 EXPECT_EQ(GURL(test.sanitized_url), | |
3628 ProxyService::SanitizeUrl(raw_url, | |
3629 ProxyService::SanitizeUrlPolicy::SAFE)); | |
3630 } | |
3631 } | |
3632 | |
3633 // Tests SanitizeUrlForPacScript() using input URLs that have a cryptographic | |
3634 // schemes (i.e. https://). The sanitized result differs depending | |
3635 // on the sanitization mode chosen. | |
3636 TEST_F(ProxyServiceTest, SanitizeUrlForPacScriptCryptographic) { | |
3637 const struct { | |
3638 // Input URL. | |
3639 const char* raw_url; | |
3640 | |
3641 // Output URL when stripping of cryptographic URLs is disabled. | |
3642 const char* sanitized_url_unstripped; | |
3643 | |
3644 // Output URL when stripping of cryptographic URLs is enabled. | |
3645 const char* sanitized_url; | |
3646 } kTests[] = { | |
3647 // Embedded identity is always stripped. | |
3648 { | |
3649 "https://foo:bar@example.com/", "https://example.com/", | |
3650 "https://example.com/", | |
3651 }, | |
3652 // Fragments are always stripped, but stripping path is conditional on the | |
3653 // mode. | |
3654 { | |
3655 "https://example.com/blah#hello", "https://example.com/blah", | |
3656 "https://example.com/", | |
3657 }, | |
3658 // Stripping the query is conditional on the mode. | |
3659 { | |
3660 "https://example.com/foo/bar/baz?hello", | |
3661 "https://example.com/foo/bar/baz?hello", "https://example.com/", | |
mmenke
2016/05/20 17:40:23
Suggest no path on this one (Well...a path of "/",
eroman
2016/05/20 21:23:53
Done.
| |
3662 }, | |
3663 // The embedded identity and fragment is always stripped, however path and | |
3664 // query are conditional on the stripping mode. | |
3665 { | |
3666 "https://foo:bar@example.com/foo/bar/baz?hello#sigh", | |
3667 "https://example.com/foo/bar/baz?hello", "https://example.com/", | |
3668 }, | |
3669 // The URL's port should not be stripped. | |
3670 { | |
3671 "https://example.com:88/hi", "https://example.com:88/hi", | |
3672 "https://example.com:88/", | |
3673 }, | |
mmenke
2016/05/20 17:40:23
Suggest also having a wss test. Could add a ws te
eroman
2016/05/20 21:23:53
Done.
| |
3674 }; | |
3675 | |
3676 for (const auto& test : kTests) { | |
3677 GURL raw_url(test.raw_url); | |
3678 ASSERT_TRUE(raw_url.is_valid()); | |
3679 EXPECT_TRUE(raw_url.SchemeIsCryptographic()); | |
3680 | |
3681 EXPECT_EQ(GURL(test.sanitized_url_unstripped), | |
3682 ProxyService::SanitizeUrl( | |
3683 raw_url, ProxyService::SanitizeUrlPolicy::UNSAFE)); | |
3684 | |
3685 EXPECT_EQ(GURL(test.sanitized_url), | |
3686 ProxyService::SanitizeUrl(raw_url, | |
3687 ProxyService::SanitizeUrlPolicy::SAFE)); | |
3688 } | |
3689 } | |
3690 | |
3483 } // namespace net | 3691 } // namespace net |
OLD | NEW |