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