| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/mojo_proxy_resolver_impl.h" | 5 #include "net/proxy/mojo_proxy_resolver_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 85 |
| 86 void TestRequestClient::LoadStateChanged(int32_t load_state) { | 86 void TestRequestClient::LoadStateChanged(int32_t load_state) { |
| 87 event_waiter_.NotifyEvent(LOAD_STATE_CHANGED); | 87 event_waiter_.NotifyEvent(LOAD_STATE_CHANGED); |
| 88 load_state_ = static_cast<LoadState>(load_state); | 88 load_state_ = static_cast<LoadState>(load_state); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void TestRequestClient::OnConnectionError() { | 91 void TestRequestClient::OnConnectionError() { |
| 92 event_waiter_.NotifyEvent(CONNECTION_ERROR); | 92 event_waiter_.NotifyEvent(CONNECTION_ERROR); |
| 93 } | 93 } |
| 94 | 94 |
| 95 class CallbackMockProxyResolver : public MockAsyncProxyResolverExpectsBytes { | 95 class CallbackMockProxyResolver : public MockAsyncProxyResolver { |
| 96 public: | 96 public: |
| 97 CallbackMockProxyResolver() {} | 97 CallbackMockProxyResolver() {} |
| 98 ~CallbackMockProxyResolver() override; | 98 ~CallbackMockProxyResolver() override; |
| 99 | 99 |
| 100 // MockAsyncProxyResolverExpectsBytes overrides. | 100 // MockAsyncProxyResolver overrides. |
| 101 int GetProxyForURL(const GURL& url, | 101 int GetProxyForURL(const GURL& url, |
| 102 ProxyInfo* results, | 102 ProxyInfo* results, |
| 103 const CompletionCallback& callback, | 103 const CompletionCallback& callback, |
| 104 RequestHandle* request_handle, | 104 RequestHandle* request_handle, |
| 105 const BoundNetLog& net_log) override; | 105 const BoundNetLog& net_log) override; |
| 106 void CancelRequest(RequestHandle request_handle) override; | 106 void CancelRequest(RequestHandle request_handle) override; |
| 107 | 107 |
| 108 // Wait until the mock resolver has received a CancelRequest call. | 108 // Wait until the mock resolver has received a CancelRequest call. |
| 109 void WaitForCancel(); | 109 void WaitForCancel(); |
| 110 | 110 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 124 const GURL& url, | 124 const GURL& url, |
| 125 ProxyInfo* results, | 125 ProxyInfo* results, |
| 126 const CompletionCallback& callback, | 126 const CompletionCallback& callback, |
| 127 RequestHandle* request_handle, | 127 RequestHandle* request_handle, |
| 128 const BoundNetLog& net_log) { | 128 const BoundNetLog& net_log) { |
| 129 if (sync_result_) { | 129 if (sync_result_) { |
| 130 *results = *sync_result_; | 130 *results = *sync_result_; |
| 131 sync_result_.reset(); | 131 sync_result_.reset(); |
| 132 return OK; | 132 return OK; |
| 133 } | 133 } |
| 134 return MockAsyncProxyResolverExpectsBytes::GetProxyForURL( | 134 return MockAsyncProxyResolver::GetProxyForURL(url, results, callback, |
| 135 url, results, callback, request_handle, net_log); | 135 request_handle, net_log); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void CallbackMockProxyResolver::CancelRequest(RequestHandle request_handle) { | 138 void CallbackMockProxyResolver::CancelRequest(RequestHandle request_handle) { |
| 139 MockAsyncProxyResolverExpectsBytes::CancelRequest(request_handle); | 139 MockAsyncProxyResolver::CancelRequest(request_handle); |
| 140 if (!cancel_callback_.is_null()) { | 140 if (!cancel_callback_.is_null()) { |
| 141 cancel_callback_.Run(); | 141 cancel_callback_.Run(); |
| 142 cancel_callback_.Reset(); | 142 cancel_callback_.Reset(); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void CallbackMockProxyResolver::WaitForCancel() { | 146 void CallbackMockProxyResolver::WaitForCancel() { |
| 147 while (cancelled_requests().empty()) { | 147 while (cancelled_requests().empty()) { |
| 148 base::RunLoop run_loop; | 148 base::RunLoop run_loop; |
| 149 cancel_callback_ = run_loop.QuitClosure(); | 149 cancel_callback_ = run_loop.QuitClosure(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 interfaces::ProxyResolver* resolver_; | 184 interfaces::ProxyResolver* resolver_; |
| 185 ProxyResolver::LoadStateChangedCallback load_state_changed_callback_; | 185 ProxyResolver::LoadStateChangedCallback load_state_changed_callback_; |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 188 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
| 189 interfaces::ProxyResolverRequestClientPtr client_ptr; | 189 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 190 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 190 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 191 | 191 |
| 192 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 192 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
| 193 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 193 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 194 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 194 scoped_refptr<MockAsyncProxyResolver::Request> request = |
| 195 mock_proxy_resolver_->pending_requests()[0]; | 195 mock_proxy_resolver_->pending_requests()[0]; |
| 196 EXPECT_EQ(GURL("http://example.com"), request->url()); | 196 EXPECT_EQ(GURL("http://example.com"), request->url()); |
| 197 | 197 |
| 198 ASSERT_FALSE(load_state_changed_callback_.is_null()); | 198 ASSERT_FALSE(load_state_changed_callback_.is_null()); |
| 199 load_state_changed_callback_.Run(request.get(), | 199 load_state_changed_callback_.Run(request.get(), |
| 200 LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | 200 LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); |
| 201 client.event_waiter().WaitForEvent(TestRequestClient::LOAD_STATE_CHANGED); | 201 client.event_waiter().WaitForEvent(TestRequestClient::LOAD_STATE_CHANGED); |
| 202 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, client.load_state()); | 202 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, client.load_state()); |
| 203 | 203 |
| 204 request->results()->UsePacString( | 204 request->results()->UsePacString( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 ProxyServer& server = proxy_servers[0]; | 256 ProxyServer& server = proxy_servers[0]; |
| 257 EXPECT_TRUE(server.is_direct()); | 257 EXPECT_TRUE(server.is_direct()); |
| 258 } | 258 } |
| 259 | 259 |
| 260 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { | 260 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { |
| 261 interfaces::ProxyResolverRequestClientPtr client_ptr; | 261 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 262 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 262 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 263 | 263 |
| 264 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 264 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
| 265 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 265 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 266 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 266 scoped_refptr<MockAsyncProxyResolver::Request> request = |
| 267 mock_proxy_resolver_->pending_requests()[0]; | 267 mock_proxy_resolver_->pending_requests()[0]; |
| 268 EXPECT_EQ(GURL("http://example.com"), request->url()); | 268 EXPECT_EQ(GURL("http://example.com"), request->url()); |
| 269 request->CompleteNow(ERR_FAILED); | 269 request->CompleteNow(ERR_FAILED); |
| 270 client.WaitForResult(); | 270 client.WaitForResult(); |
| 271 | 271 |
| 272 EXPECT_EQ(ERR_FAILED, client.error()); | 272 EXPECT_EQ(ERR_FAILED, client.error()); |
| 273 std::vector<ProxyServer> proxy_servers = | 273 std::vector<ProxyServer> proxy_servers = |
| 274 client.results().To<std::vector<ProxyServer>>(); | 274 client.results().To<std::vector<ProxyServer>>(); |
| 275 EXPECT_TRUE(proxy_servers.empty()); | 275 EXPECT_TRUE(proxy_servers.empty()); |
| 276 } | 276 } |
| 277 | 277 |
| 278 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { | 278 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { |
| 279 interfaces::ProxyResolverRequestClientPtr client_ptr1; | 279 interfaces::ProxyResolverRequestClientPtr client_ptr1; |
| 280 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); | 280 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); |
| 281 interfaces::ProxyResolverRequestClientPtr client_ptr2; | 281 interfaces::ProxyResolverRequestClientPtr client_ptr2; |
| 282 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); | 282 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); |
| 283 | 283 |
| 284 resolver_->GetProxyForUrl("http://example.com", client_ptr1.Pass()); | 284 resolver_->GetProxyForUrl("http://example.com", client_ptr1.Pass()); |
| 285 resolver_->GetProxyForUrl("https://example.com", client_ptr2.Pass()); | 285 resolver_->GetProxyForUrl("https://example.com", client_ptr2.Pass()); |
| 286 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); | 286 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); |
| 287 scoped_refptr<MockAsyncProxyResolverBase::Request> request1 = | 287 scoped_refptr<MockAsyncProxyResolver::Request> request1 = |
| 288 mock_proxy_resolver_->pending_requests()[0]; | 288 mock_proxy_resolver_->pending_requests()[0]; |
| 289 EXPECT_EQ(GURL("http://example.com"), request1->url()); | 289 EXPECT_EQ(GURL("http://example.com"), request1->url()); |
| 290 scoped_refptr<MockAsyncProxyResolverBase::Request> request2 = | 290 scoped_refptr<MockAsyncProxyResolver::Request> request2 = |
| 291 mock_proxy_resolver_->pending_requests()[1]; | 291 mock_proxy_resolver_->pending_requests()[1]; |
| 292 EXPECT_EQ(GURL("https://example.com"), request2->url()); | 292 EXPECT_EQ(GURL("https://example.com"), request2->url()); |
| 293 request1->results()->UsePacString("HTTPS proxy.example.com:12345"); | 293 request1->results()->UsePacString("HTTPS proxy.example.com:12345"); |
| 294 request1->CompleteNow(OK); | 294 request1->CompleteNow(OK); |
| 295 request2->results()->UsePacString("SOCKS5 another-proxy.example.com:6789"); | 295 request2->results()->UsePacString("SOCKS5 another-proxy.example.com:6789"); |
| 296 request2->CompleteNow(OK); | 296 request2->CompleteNow(OK); |
| 297 client1.WaitForResult(); | 297 client1.WaitForResult(); |
| 298 client2.WaitForResult(); | 298 client2.WaitForResult(); |
| 299 | 299 |
| 300 EXPECT_EQ(OK, client1.error()); | 300 EXPECT_EQ(OK, client1.error()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 316 EXPECT_EQ(6789, server2.host_port_pair().port()); | 316 EXPECT_EQ(6789, server2.host_port_pair().port()); |
| 317 } | 317 } |
| 318 | 318 |
| 319 TEST_F(MojoProxyResolverImplTest, DestroyClient) { | 319 TEST_F(MojoProxyResolverImplTest, DestroyClient) { |
| 320 interfaces::ProxyResolverRequestClientPtr client_ptr; | 320 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 321 scoped_ptr<TestRequestClient> client( | 321 scoped_ptr<TestRequestClient> client( |
| 322 new TestRequestClient(mojo::GetProxy(&client_ptr))); | 322 new TestRequestClient(mojo::GetProxy(&client_ptr))); |
| 323 | 323 |
| 324 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 324 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
| 325 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 325 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 326 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 326 scoped_refptr<MockAsyncProxyResolver::Request> request = |
| 327 mock_proxy_resolver_->pending_requests()[0]; | 327 mock_proxy_resolver_->pending_requests()[0]; |
| 328 EXPECT_EQ(GURL("http://example.com"), request->url()); | 328 EXPECT_EQ(GURL("http://example.com"), request->url()); |
| 329 request->results()->UsePacString("PROXY proxy.example.com:8080"); | 329 request->results()->UsePacString("PROXY proxy.example.com:8080"); |
| 330 client.reset(); | 330 client.reset(); |
| 331 mock_proxy_resolver_->WaitForCancel(); | 331 mock_proxy_resolver_->WaitForCancel(); |
| 332 } | 332 } |
| 333 | 333 |
| 334 TEST_F(MojoProxyResolverImplTest, DestroyService) { | 334 TEST_F(MojoProxyResolverImplTest, DestroyService) { |
| 335 interfaces::ProxyResolverRequestClientPtr client_ptr; | 335 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 336 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 336 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 337 | 337 |
| 338 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 338 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
| 339 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 339 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 340 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 340 scoped_refptr<MockAsyncProxyResolver::Request> request = |
| 341 mock_proxy_resolver_->pending_requests()[0]; | 341 mock_proxy_resolver_->pending_requests()[0]; |
| 342 resolver_impl_.reset(); | 342 resolver_impl_.reset(); |
| 343 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 343 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
| 344 } | 344 } |
| 345 | 345 |
| 346 } // namespace net | 346 } // namespace net |
| OLD | NEW |