| 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 <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/proxy/mock_proxy_resolver.h" | 14 #include "net/proxy/mock_proxy_resolver.h" |
| 15 #include "net/proxy/mojo_proxy_type_converters.h" | |
| 16 #include "net/proxy/proxy_info.h" | 15 #include "net/proxy/proxy_info.h" |
| 17 #include "net/proxy/proxy_resolver_v8_tracing.h" | 16 #include "net/proxy/proxy_resolver_v8_tracing.h" |
| 18 #include "net/proxy/proxy_server.h" | 17 #include "net/proxy/proxy_server.h" |
| 19 #include "net/test/event_waiter.h" | 18 #include "net/test/event_waiter.h" |
| 20 #include "net/test/gtest_util.h" | 19 #include "net/test/gtest_util.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 22 |
| 24 using net::test::IsError; | 23 using net::test::IsError; |
| 25 using net::test::IsOk; | 24 using net::test::IsOk; |
| 26 | 25 |
| 27 namespace net { | 26 namespace net { |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 class TestRequestClient : public interfaces::ProxyResolverRequestClient { | 29 class TestRequestClient : public interfaces::ProxyResolverRequestClient { |
| 31 public: | 30 public: |
| 32 enum Event { | 31 enum Event { |
| 33 RESULT_RECEIVED, | 32 RESULT_RECEIVED, |
| 34 CONNECTION_ERROR, | 33 CONNECTION_ERROR, |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 explicit TestRequestClient( | 36 explicit TestRequestClient( |
| 38 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); | 37 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); |
| 39 | 38 |
| 40 void WaitForResult(); | 39 void WaitForResult(); |
| 41 | 40 |
| 42 Error error() { return error_; } | 41 Error error() { return error_; } |
| 43 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } | 42 const ProxyInfo& results() { return results_; } |
| 44 EventWaiter<Event>& event_waiter() { return event_waiter_; } | 43 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
| 45 | 44 |
| 46 private: | 45 private: |
| 47 // interfaces::ProxyResolverRequestClient override. | 46 // interfaces::ProxyResolverRequestClient override. |
| 48 void ReportResult(int32_t error, | 47 void ReportResult(int32_t error, const ProxyInfo& results) override; |
| 49 mojo::Array<interfaces::ProxyServerPtr> results) override; | |
| 50 void Alert(const mojo::String& message) override; | 48 void Alert(const mojo::String& message) override; |
| 51 void OnError(int32_t line_number, const mojo::String& message) override; | 49 void OnError(int32_t line_number, const mojo::String& message) override; |
| 52 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info, | 50 void ResolveDns(std::unique_ptr<HostResolver::RequestInfo> request_info, |
| 53 interfaces::HostResolverRequestClientPtr client) override; | 51 interfaces::HostResolverRequestClientPtr client) override; |
| 54 | 52 |
| 55 // Mojo error handler. | 53 // Mojo error handler. |
| 56 void OnConnectionError(); | 54 void OnConnectionError(); |
| 57 | 55 |
| 58 bool done_ = false; | 56 bool done_ = false; |
| 59 Error error_ = ERR_FAILED; | 57 Error error_ = ERR_FAILED; |
| 60 mojo::Array<interfaces::ProxyServerPtr> results_; | 58 ProxyInfo results_; |
| 61 | 59 |
| 62 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 60 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
| 63 | 61 |
| 64 EventWaiter<Event> event_waiter_; | 62 EventWaiter<Event> event_waiter_; |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 TestRequestClient::TestRequestClient( | 65 TestRequestClient::TestRequestClient( |
| 68 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) | 66 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) |
| 69 : binding_(this, std::move(request)) { | 67 : binding_(this, std::move(request)) { |
| 70 binding_.set_connection_error_handler(base::Bind( | 68 binding_.set_connection_error_handler(base::Bind( |
| 71 &TestRequestClient::OnConnectionError, base::Unretained(this))); | 69 &TestRequestClient::OnConnectionError, base::Unretained(this))); |
| 72 } | 70 } |
| 73 | 71 |
| 74 void TestRequestClient::WaitForResult() { | 72 void TestRequestClient::WaitForResult() { |
| 75 if (done_) | 73 if (done_) |
| 76 return; | 74 return; |
| 77 | 75 |
| 78 event_waiter_.WaitForEvent(RESULT_RECEIVED); | 76 event_waiter_.WaitForEvent(RESULT_RECEIVED); |
| 79 ASSERT_TRUE(done_); | 77 ASSERT_TRUE(done_); |
| 80 } | 78 } |
| 81 | 79 |
| 82 void TestRequestClient::ReportResult( | 80 void TestRequestClient::ReportResult(int32_t error, const ProxyInfo& results) { |
| 83 int32_t error, | |
| 84 mojo::Array<interfaces::ProxyServerPtr> results) { | |
| 85 event_waiter_.NotifyEvent(RESULT_RECEIVED); | 81 event_waiter_.NotifyEvent(RESULT_RECEIVED); |
| 86 ASSERT_FALSE(done_); | 82 ASSERT_FALSE(done_); |
| 87 error_ = static_cast<Error>(error); | 83 error_ = static_cast<Error>(error); |
| 88 results_ = std::move(results); | 84 results_ = results; |
| 89 done_ = true; | 85 done_ = true; |
| 90 } | 86 } |
| 91 | 87 |
| 92 void TestRequestClient::Alert(const mojo::String& message) { | 88 void TestRequestClient::Alert(const mojo::String& message) { |
| 93 } | 89 } |
| 94 | 90 |
| 95 void TestRequestClient::OnError(int32_t line_number, | 91 void TestRequestClient::OnError(int32_t line_number, |
| 96 const mojo::String& message) { | 92 const mojo::String& message) { |
| 97 } | 93 } |
| 98 | 94 |
| 99 void TestRequestClient::ResolveDns( | 95 void TestRequestClient::ResolveDns( |
| 100 interfaces::HostResolverRequestInfoPtr request_info, | 96 std::unique_ptr<HostResolver::RequestInfo> request_info, |
| 101 interfaces::HostResolverRequestClientPtr client) { | 97 interfaces::HostResolverRequestClientPtr client) {} |
| 102 } | |
| 103 | 98 |
| 104 void TestRequestClient::OnConnectionError() { | 99 void TestRequestClient::OnConnectionError() { |
| 105 event_waiter_.NotifyEvent(CONNECTION_ERROR); | 100 event_waiter_.NotifyEvent(CONNECTION_ERROR); |
| 106 } | 101 } |
| 107 | 102 |
| 108 class MockProxyResolverV8Tracing : public ProxyResolverV8Tracing { | 103 class MockProxyResolverV8Tracing : public ProxyResolverV8Tracing { |
| 109 public: | 104 public: |
| 110 struct Request { | 105 struct Request { |
| 111 GURL url; | 106 GURL url; |
| 112 ProxyInfo* results; | 107 ProxyInfo* results; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 "PROXY proxy.example.com:1; " | 202 "PROXY proxy.example.com:1; " |
| 208 "SOCKS4 socks4.example.com:2; " | 203 "SOCKS4 socks4.example.com:2; " |
| 209 "SOCKS5 socks5.example.com:3; " | 204 "SOCKS5 socks5.example.com:3; " |
| 210 "HTTPS https.example.com:4; " | 205 "HTTPS https.example.com:4; " |
| 211 "QUIC quic.example.com:65000; " | 206 "QUIC quic.example.com:65000; " |
| 212 "DIRECT"); | 207 "DIRECT"); |
| 213 request.callback.Run(OK); | 208 request.callback.Run(OK); |
| 214 client.WaitForResult(); | 209 client.WaitForResult(); |
| 215 | 210 |
| 216 EXPECT_THAT(client.error(), IsOk()); | 211 EXPECT_THAT(client.error(), IsOk()); |
| 217 std::vector<ProxyServer> servers = | 212 std::vector<ProxyServer> servers = client.results().proxy_list().GetAll(); |
| 218 client.results().To<std::vector<ProxyServer>>(); | |
| 219 ASSERT_EQ(6u, servers.size()); | 213 ASSERT_EQ(6u, servers.size()); |
| 220 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); | 214 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); |
| 221 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); | 215 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); |
| 222 EXPECT_EQ(1, servers[0].host_port_pair().port()); | 216 EXPECT_EQ(1, servers[0].host_port_pair().port()); |
| 223 | 217 |
| 224 EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, servers[1].scheme()); | 218 EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, servers[1].scheme()); |
| 225 EXPECT_EQ("socks4.example.com", servers[1].host_port_pair().host()); | 219 EXPECT_EQ("socks4.example.com", servers[1].host_port_pair().host()); |
| 226 EXPECT_EQ(2, servers[1].host_port_pair().port()); | 220 EXPECT_EQ(2, servers[1].host_port_pair().port()); |
| 227 | 221 |
| 228 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, servers[2].scheme()); | 222 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, servers[2].scheme()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 247 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); | 241 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); |
| 248 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 242 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 249 const MockProxyResolverV8Tracing::Request& request = | 243 const MockProxyResolverV8Tracing::Request& request = |
| 250 mock_proxy_resolver_->pending_requests()[0]; | 244 mock_proxy_resolver_->pending_requests()[0]; |
| 251 EXPECT_EQ(GURL("http://example.com"), request.url); | 245 EXPECT_EQ(GURL("http://example.com"), request.url); |
| 252 request.callback.Run(ERR_FAILED); | 246 request.callback.Run(ERR_FAILED); |
| 253 client.WaitForResult(); | 247 client.WaitForResult(); |
| 254 | 248 |
| 255 EXPECT_THAT(client.error(), IsError(ERR_FAILED)); | 249 EXPECT_THAT(client.error(), IsError(ERR_FAILED)); |
| 256 std::vector<ProxyServer> proxy_servers = | 250 std::vector<ProxyServer> proxy_servers = |
| 257 client.results().To<std::vector<ProxyServer>>(); | 251 client.results().proxy_list().GetAll(); |
| 258 EXPECT_TRUE(proxy_servers.empty()); | 252 EXPECT_TRUE(proxy_servers.empty()); |
| 259 } | 253 } |
| 260 | 254 |
| 261 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { | 255 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { |
| 262 interfaces::ProxyResolverRequestClientPtr client_ptr1; | 256 interfaces::ProxyResolverRequestClientPtr client_ptr1; |
| 263 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); | 257 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); |
| 264 interfaces::ProxyResolverRequestClientPtr client_ptr2; | 258 interfaces::ProxyResolverRequestClientPtr client_ptr2; |
| 265 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); | 259 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); |
| 266 | 260 |
| 267 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr1)); | 261 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr1)); |
| 268 resolver_->GetProxyForUrl(GURL("https://example.com"), | 262 resolver_->GetProxyForUrl(GURL("https://example.com"), |
| 269 std::move(client_ptr2)); | 263 std::move(client_ptr2)); |
| 270 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); | 264 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); |
| 271 const MockProxyResolverV8Tracing::Request& request1 = | 265 const MockProxyResolverV8Tracing::Request& request1 = |
| 272 mock_proxy_resolver_->pending_requests()[0]; | 266 mock_proxy_resolver_->pending_requests()[0]; |
| 273 EXPECT_EQ(GURL("http://example.com"), request1.url); | 267 EXPECT_EQ(GURL("http://example.com"), request1.url); |
| 274 const MockProxyResolverV8Tracing::Request& request2 = | 268 const MockProxyResolverV8Tracing::Request& request2 = |
| 275 mock_proxy_resolver_->pending_requests()[1]; | 269 mock_proxy_resolver_->pending_requests()[1]; |
| 276 EXPECT_EQ(GURL("https://example.com"), request2.url); | 270 EXPECT_EQ(GURL("https://example.com"), request2.url); |
| 277 request1.results->UsePacString("HTTPS proxy.example.com:12345"); | 271 request1.results->UsePacString("HTTPS proxy.example.com:12345"); |
| 278 request1.callback.Run(OK); | 272 request1.callback.Run(OK); |
| 279 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); | 273 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); |
| 280 request2.callback.Run(OK); | 274 request2.callback.Run(OK); |
| 281 client1.WaitForResult(); | 275 client1.WaitForResult(); |
| 282 client2.WaitForResult(); | 276 client2.WaitForResult(); |
| 283 | 277 |
| 284 EXPECT_THAT(client1.error(), IsOk()); | 278 EXPECT_THAT(client1.error(), IsOk()); |
| 285 std::vector<ProxyServer> proxy_servers1 = | 279 std::vector<ProxyServer> proxy_servers1 = |
| 286 client1.results().To<std::vector<ProxyServer>>(); | 280 client1.results().proxy_list().GetAll(); |
| 287 ASSERT_EQ(1u, proxy_servers1.size()); | 281 ASSERT_EQ(1u, proxy_servers1.size()); |
| 288 ProxyServer& server1 = proxy_servers1[0]; | 282 ProxyServer& server1 = proxy_servers1[0]; |
| 289 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); | 283 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); |
| 290 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); | 284 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); |
| 291 EXPECT_EQ(12345, server1.host_port_pair().port()); | 285 EXPECT_EQ(12345, server1.host_port_pair().port()); |
| 292 | 286 |
| 293 EXPECT_THAT(client2.error(), IsOk()); | 287 EXPECT_THAT(client2.error(), IsOk()); |
| 294 std::vector<ProxyServer> proxy_servers2 = | 288 std::vector<ProxyServer> proxy_servers2 = |
| 295 client2.results().To<std::vector<ProxyServer>>(); | 289 client2.results().proxy_list().GetAll(); |
| 296 ASSERT_EQ(1u, proxy_servers1.size()); | 290 ASSERT_EQ(1u, proxy_servers1.size()); |
| 297 ProxyServer& server2 = proxy_servers2[0]; | 291 ProxyServer& server2 = proxy_servers2[0]; |
| 298 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); | 292 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); |
| 299 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); | 293 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); |
| 300 EXPECT_EQ(6789, server2.host_port_pair().port()); | 294 EXPECT_EQ(6789, server2.host_port_pair().port()); |
| 301 } | 295 } |
| 302 | 296 |
| 303 TEST_F(MojoProxyResolverImplTest, DestroyClient) { | 297 TEST_F(MojoProxyResolverImplTest, DestroyClient) { |
| 304 interfaces::ProxyResolverRequestClientPtr client_ptr; | 298 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 305 std::unique_ptr<TestRequestClient> client( | 299 std::unique_ptr<TestRequestClient> client( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 319 interfaces::ProxyResolverRequestClientPtr client_ptr; | 313 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 320 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 314 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 321 | 315 |
| 322 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); | 316 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); |
| 323 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 317 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 324 resolver_impl_.reset(); | 318 resolver_impl_.reset(); |
| 325 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 319 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
| 326 } | 320 } |
| 327 | 321 |
| 328 } // namespace net | 322 } // namespace net |
| OLD | NEW |