| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_resolver_v8_tracing_wrapper.h" | 5 #include "net/proxy/proxy_resolver_v8_tracing_wrapper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 681 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 682 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); | 682 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); |
| 683 | 683 |
| 684 const size_t kNumRequests = 5; | 684 const size_t kNumRequests = 5; |
| 685 ProxyInfo proxy_info[kNumRequests]; | 685 ProxyInfo proxy_info[kNumRequests]; |
| 686 ProxyResolver::RequestHandle request[kNumRequests]; | 686 ProxyResolver::RequestHandle request[kNumRequests]; |
| 687 | 687 |
| 688 for (size_t i = 0; i < kNumRequests; ++i) { | 688 for (size_t i = 0; i < kNumRequests; ++i) { |
| 689 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], | 689 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], |
| 690 base::Bind(&CrashCallback), &request[i], | 690 base::Bind(&CrashCallback), &request[i], |
| 691 BoundNetLog()); | 691 NetLogWithSource()); |
| 692 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 692 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 693 } | 693 } |
| 694 | 694 |
| 695 for (size_t i = 0; i < kNumRequests; ++i) { | 695 for (size_t i = 0; i < kNumRequests; ++i) { |
| 696 resolver->CancelRequest(request[i]); | 696 resolver->CancelRequest(request[i]); |
| 697 } | 697 } |
| 698 } | 698 } |
| 699 | 699 |
| 700 // Note the execution order for this test can vary. Since multiple | 700 // Note the execution order for this test can vary. Since multiple |
| 701 // threads are involved, the cancellation may be received a different | 701 // threads are involved, the cancellation may be received a different |
| 702 // times. | 702 // times. |
| 703 TEST_F(ProxyResolverV8TracingWrapperTest, CancelSome) { | 703 TEST_F(ProxyResolverV8TracingWrapperTest, CancelSome) { |
| 704 MockCachingHostResolver host_resolver; | 704 MockCachingHostResolver host_resolver; |
| 705 MockErrorObserver* error_observer = new MockErrorObserver; | 705 MockErrorObserver* error_observer = new MockErrorObserver; |
| 706 | 706 |
| 707 host_resolver.rules()->AddSimulatedFailure("*"); | 707 host_resolver.rules()->AddSimulatedFailure("*"); |
| 708 | 708 |
| 709 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 709 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 710 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); | 710 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); |
| 711 | 711 |
| 712 ProxyInfo proxy_info1; | 712 ProxyInfo proxy_info1; |
| 713 ProxyInfo proxy_info2; | 713 ProxyInfo proxy_info2; |
| 714 ProxyResolver::RequestHandle request1; | 714 ProxyResolver::RequestHandle request1; |
| 715 ProxyResolver::RequestHandle request2; | 715 ProxyResolver::RequestHandle request2; |
| 716 TestCompletionCallback callback; | 716 TestCompletionCallback callback; |
| 717 | 717 |
| 718 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, | 718 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, |
| 719 base::Bind(&CrashCallback), &request1, | 719 base::Bind(&CrashCallback), &request1, |
| 720 BoundNetLog()); | 720 NetLogWithSource()); |
| 721 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 721 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 722 | 722 |
| 723 rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, | 723 rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, |
| 724 callback.callback(), &request2, BoundNetLog()); | 724 callback.callback(), &request2, |
| 725 NetLogWithSource()); |
| 725 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 726 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 726 | 727 |
| 727 resolver->CancelRequest(request1); | 728 resolver->CancelRequest(request1); |
| 728 | 729 |
| 729 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 730 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 730 } | 731 } |
| 731 | 732 |
| 732 // Cancel a request after it has finished running on the worker thread, and has | 733 // Cancel a request after it has finished running on the worker thread, and has |
| 733 // posted a task the completion task back to origin thread. | 734 // posted a task the completion task back to origin thread. |
| 734 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhilePendingCompletionTask) { | 735 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhilePendingCompletionTask) { |
| 735 MockCachingHostResolver host_resolver; | 736 MockCachingHostResolver host_resolver; |
| 736 MockErrorObserver* error_observer = new MockErrorObserver; | 737 MockErrorObserver* error_observer = new MockErrorObserver; |
| 737 | 738 |
| 738 host_resolver.rules()->AddSimulatedFailure("*"); | 739 host_resolver.rules()->AddSimulatedFailure("*"); |
| 739 | 740 |
| 740 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 741 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 741 nullptr, &host_resolver, base::WrapUnique(error_observer), "error.js"); | 742 nullptr, &host_resolver, base::WrapUnique(error_observer), "error.js"); |
| 742 | 743 |
| 743 ProxyInfo proxy_info1; | 744 ProxyInfo proxy_info1; |
| 744 ProxyInfo proxy_info2; | 745 ProxyInfo proxy_info2; |
| 745 ProxyResolver::RequestHandle request1; | 746 ProxyResolver::RequestHandle request1; |
| 746 ProxyResolver::RequestHandle request2; | 747 ProxyResolver::RequestHandle request2; |
| 747 TestCompletionCallback callback; | 748 TestCompletionCallback callback; |
| 748 | 749 |
| 749 int rv = resolver->GetProxyForURL(GURL("http://throw-an-error/"), | 750 int rv = resolver->GetProxyForURL(GURL("http://throw-an-error/"), |
| 750 &proxy_info1, base::Bind(&CrashCallback), | 751 &proxy_info1, base::Bind(&CrashCallback), |
| 751 &request1, BoundNetLog()); | 752 &request1, NetLogWithSource()); |
| 752 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 753 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 753 | 754 |
| 754 // Wait until the first request has finished running on the worker thread. | 755 // Wait until the first request has finished running on the worker thread. |
| 755 // Cancel the first request, while it has a pending completion task on | 756 // Cancel the first request, while it has a pending completion task on |
| 756 // the origin thread. | 757 // the origin thread. |
| 757 error_observer->RunOnError(base::Bind(&ProxyResolver::CancelRequest, | 758 error_observer->RunOnError(base::Bind(&ProxyResolver::CancelRequest, |
| 758 base::Unretained(resolver.get()), | 759 base::Unretained(resolver.get()), |
| 759 request1)); | 760 request1)); |
| 760 | 761 |
| 761 // Start another request, to make sure it is able to complete. | 762 // Start another request, to make sure it is able to complete. |
| 762 rv = resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), | 763 rv = resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), |
| 763 &proxy_info2, callback.callback(), &request2, | 764 &proxy_info2, callback.callback(), &request2, |
| 764 BoundNetLog()); | 765 NetLogWithSource()); |
| 765 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 766 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 766 | 767 |
| 767 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 768 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 768 | 769 |
| 769 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI()); | 770 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI()); |
| 770 } | 771 } |
| 771 | 772 |
| 772 // This implementation of HostResolver allows blocking until a resolve request | 773 // This implementation of HostResolver allows blocking until a resolve request |
| 773 // has been received. The resolve requests it receives will never be completed. | 774 // has been received. The resolve requests it receives will never be completed. |
| 774 class BlockableHostResolver : public HostResolver { | 775 class BlockableHostResolver : public HostResolver { |
| 775 public: | 776 public: |
| 776 BlockableHostResolver() | 777 BlockableHostResolver() |
| 777 : num_cancelled_requests_(0), waiting_for_resolve_(false) {} | 778 : num_cancelled_requests_(0), waiting_for_resolve_(false) {} |
| 778 | 779 |
| 779 int Resolve(const RequestInfo& info, | 780 int Resolve(const RequestInfo& info, |
| 780 RequestPriority priority, | 781 RequestPriority priority, |
| 781 AddressList* addresses, | 782 AddressList* addresses, |
| 782 const CompletionCallback& callback, | 783 const CompletionCallback& callback, |
| 783 std::unique_ptr<Request>* out_req, | 784 std::unique_ptr<Request>* out_req, |
| 784 const BoundNetLog& net_log) override { | 785 const NetLogWithSource& net_log) override { |
| 785 EXPECT_FALSE(callback.is_null()); | 786 EXPECT_FALSE(callback.is_null()); |
| 786 EXPECT_TRUE(out_req); | 787 EXPECT_TRUE(out_req); |
| 787 | 788 |
| 788 if (!action_.is_null()) | 789 if (!action_.is_null()) |
| 789 action_.Run(); | 790 action_.Run(); |
| 790 | 791 |
| 791 // Indicate to the caller that a request was received. | 792 // Indicate to the caller that a request was received. |
| 792 EXPECT_TRUE(waiting_for_resolve_); | 793 EXPECT_TRUE(waiting_for_resolve_); |
| 793 base::MessageLoop::current()->QuitWhenIdle(); | 794 base::MessageLoop::current()->QuitWhenIdle(); |
| 794 | 795 |
| 795 // This line is intentionally after action_.Run(), since one of the | 796 // This line is intentionally after action_.Run(), since one of the |
| 796 // tests does a cancellation inside of Resolve(), and it is more | 797 // tests does a cancellation inside of Resolve(), and it is more |
| 797 // interesting if *out_req hasn't been written yet at that point. | 798 // interesting if *out_req hasn't been written yet at that point. |
| 798 out_req->reset(new RequestImpl(this)); | 799 out_req->reset(new RequestImpl(this)); |
| 799 | 800 |
| 800 // Return ERR_IO_PENDING as this request will NEVER be completed. | 801 // Return ERR_IO_PENDING as this request will NEVER be completed. |
| 801 // Expectation is for the caller to later cancel the request. | 802 // Expectation is for the caller to later cancel the request. |
| 802 return ERR_IO_PENDING; | 803 return ERR_IO_PENDING; |
| 803 } | 804 } |
| 804 | 805 |
| 805 int ResolveFromCache(const RequestInfo& info, | 806 int ResolveFromCache(const RequestInfo& info, |
| 806 AddressList* addresses, | 807 AddressList* addresses, |
| 807 const BoundNetLog& net_log) override { | 808 const NetLogWithSource& net_log) override { |
| 808 NOTREACHED(); | 809 NOTREACHED(); |
| 809 return ERR_DNS_CACHE_MISS; | 810 return ERR_DNS_CACHE_MISS; |
| 810 } | 811 } |
| 811 | 812 |
| 812 void IncreaseNumOfCancelledRequests() { num_cancelled_requests_++; } | 813 void IncreaseNumOfCancelledRequests() { num_cancelled_requests_++; } |
| 813 | 814 |
| 814 void SetAction(const base::Callback<void(void)>& action) { action_ = action; } | 815 void SetAction(const base::Callback<void(void)>& action) { action_ = action; } |
| 815 | 816 |
| 816 // Waits until Resolve() has been called. | 817 // Waits until Resolve() has been called. |
| 817 void WaitUntilRequestIsReceived() { | 818 void WaitUntilRequestIsReceived() { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 857 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 857 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); | 858 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); |
| 858 | 859 |
| 859 ProxyInfo proxy_info1; | 860 ProxyInfo proxy_info1; |
| 860 ProxyInfo proxy_info2; | 861 ProxyInfo proxy_info2; |
| 861 ProxyResolver::RequestHandle request1; | 862 ProxyResolver::RequestHandle request1; |
| 862 ProxyResolver::RequestHandle request2; | 863 ProxyResolver::RequestHandle request2; |
| 863 | 864 |
| 864 int rv = resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, | 865 int rv = resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, |
| 865 base::Bind(&CrashCallback), &request1, | 866 base::Bind(&CrashCallback), &request1, |
| 866 BoundNetLog()); | 867 NetLogWithSource()); |
| 867 | 868 |
| 868 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 869 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 869 | 870 |
| 870 host_resolver.WaitUntilRequestIsReceived(); | 871 host_resolver.WaitUntilRequestIsReceived(); |
| 871 | 872 |
| 872 rv = resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, | 873 rv = resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, |
| 873 base::Bind(&CrashCallback), &request2, | 874 base::Bind(&CrashCallback), &request2, |
| 874 BoundNetLog()); | 875 NetLogWithSource()); |
| 875 | 876 |
| 876 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 877 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 877 | 878 |
| 878 host_resolver.WaitUntilRequestIsReceived(); | 879 host_resolver.WaitUntilRequestIsReceived(); |
| 879 | 880 |
| 880 resolver->CancelRequest(request1); | 881 resolver->CancelRequest(request1); |
| 881 resolver->CancelRequest(request2); | 882 resolver->CancelRequest(request2); |
| 882 | 883 |
| 883 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); | 884 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); |
| 884 | 885 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 905 MockErrorObserver* error_observer = new MockErrorObserver; | 906 MockErrorObserver* error_observer = new MockErrorObserver; |
| 906 | 907 |
| 907 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 908 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 908 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); | 909 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); |
| 909 | 910 |
| 910 ProxyInfo proxy_info; | 911 ProxyInfo proxy_info; |
| 911 ProxyResolver::RequestHandle request; | 912 ProxyResolver::RequestHandle request; |
| 912 | 913 |
| 913 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 914 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
| 914 base::Bind(&CrashCallback), &request, | 915 base::Bind(&CrashCallback), &request, |
| 915 BoundNetLog()); | 916 NetLogWithSource()); |
| 916 | 917 |
| 917 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 918 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 918 | 919 |
| 919 host_resolver.SetAction( | 920 host_resolver.SetAction( |
| 920 base::Bind(CancelRequestAndPause, resolver.get(), request)); | 921 base::Bind(CancelRequestAndPause, resolver.get(), request)); |
| 921 | 922 |
| 922 host_resolver.WaitUntilRequestIsReceived(); | 923 host_resolver.WaitUntilRequestIsReceived(); |
| 923 } | 924 } |
| 924 | 925 |
| 925 // Cancel the request while there is a pending DNS request, however before | 926 // Cancel the request while there is a pending DNS request, however before |
| 926 // the request is sent to the host resolver. | 927 // the request is sent to the host resolver. |
| 927 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns2) { | 928 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns2) { |
| 928 MockCachingHostResolver host_resolver; | 929 MockCachingHostResolver host_resolver; |
| 929 MockErrorObserver* error_observer = new MockErrorObserver; | 930 MockErrorObserver* error_observer = new MockErrorObserver; |
| 930 | 931 |
| 931 std::unique_ptr<ProxyResolver> resolver = CreateResolver( | 932 std::unique_ptr<ProxyResolver> resolver = CreateResolver( |
| 932 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); | 933 nullptr, &host_resolver, base::WrapUnique(error_observer), "dns.js"); |
| 933 | 934 |
| 934 ProxyInfo proxy_info; | 935 ProxyInfo proxy_info; |
| 935 ProxyResolver::RequestHandle request; | 936 ProxyResolver::RequestHandle request; |
| 936 | 937 |
| 937 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 938 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
| 938 base::Bind(&CrashCallback), &request, | 939 base::Bind(&CrashCallback), &request, |
| 939 BoundNetLog()); | 940 NetLogWithSource()); |
| 940 | 941 |
| 941 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 942 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 942 | 943 |
| 943 // Wait a bit, so the DNS task has hopefully been posted. The test will | 944 // Wait a bit, so the DNS task has hopefully been posted. The test will |
| 944 // work whatever the delay is here, but it is most useful if the delay | 945 // work whatever the delay is here, but it is most useful if the delay |
| 945 // is large enough to allow a task to be posted back. | 946 // is large enough to allow a task to be posted back. |
| 946 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 947 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 947 resolver->CancelRequest(request); | 948 resolver->CancelRequest(request); |
| 948 | 949 |
| 949 EXPECT_EQ(0u, host_resolver.num_resolve()); | 950 EXPECT_EQ(0u, host_resolver.num_resolve()); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1109 const size_t kNumResolvers = arraysize(resolver); | 1110 const size_t kNumResolvers = arraysize(resolver); |
| 1110 const size_t kNumIterations = 20; | 1111 const size_t kNumIterations = 20; |
| 1111 const size_t kNumResults = kNumResolvers * kNumIterations; | 1112 const size_t kNumResults = kNumResolvers * kNumIterations; |
| 1112 TestCompletionCallback callback[kNumResults]; | 1113 TestCompletionCallback callback[kNumResults]; |
| 1113 ProxyInfo proxy_info[kNumResults]; | 1114 ProxyInfo proxy_info[kNumResults]; |
| 1114 | 1115 |
| 1115 for (size_t i = 0; i < kNumResults; ++i) { | 1116 for (size_t i = 0; i < kNumResults; ++i) { |
| 1116 size_t resolver_i = i % kNumResolvers; | 1117 size_t resolver_i = i % kNumResolvers; |
| 1117 int rv = resolver[resolver_i]->GetProxyForURL( | 1118 int rv = resolver[resolver_i]->GetProxyForURL( |
| 1118 GURL("http://foo/"), &proxy_info[i], callback[i].callback(), NULL, | 1119 GURL("http://foo/"), &proxy_info[i], callback[i].callback(), NULL, |
| 1119 BoundNetLog()); | 1120 NetLogWithSource()); |
| 1120 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1121 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1121 } | 1122 } |
| 1122 | 1123 |
| 1123 // ------------------------ | 1124 // ------------------------ |
| 1124 // Verify all of the results. | 1125 // Verify all of the results. |
| 1125 // ------------------------ | 1126 // ------------------------ |
| 1126 | 1127 |
| 1127 const char* kExpectedForDnsJs = | 1128 const char* kExpectedForDnsJs = |
| 1128 "122.133.144.155-" // myIpAddress() | 1129 "122.133.144.155-" // myIpAddress() |
| 1129 "null-" // dnsResolve('') | 1130 "null-" // dnsResolve('') |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1155 proxy_uri.substr(0, proxy_uri.find(':') + 1)); | 1156 proxy_uri.substr(0, proxy_uri.find(':') + 1)); |
| 1156 } else { | 1157 } else { |
| 1157 NOTREACHED(); | 1158 NOTREACHED(); |
| 1158 } | 1159 } |
| 1159 } | 1160 } |
| 1160 } | 1161 } |
| 1161 | 1162 |
| 1162 } // namespace | 1163 } // namespace |
| 1163 | 1164 |
| 1164 } // namespace net | 1165 } // namespace net |
| OLD | NEW |