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 <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/proxy/mock_proxy_resolver.h" | 14 #include "net/proxy/mock_proxy_resolver.h" |
14 #include "net/proxy/mojo_proxy_type_converters.h" | 15 #include "net/proxy/mojo_proxy_type_converters.h" |
15 #include "net/proxy/proxy_info.h" | 16 #include "net/proxy/proxy_info.h" |
16 #include "net/proxy/proxy_resolver_v8_tracing.h" | 17 #include "net/proxy/proxy_resolver_v8_tracing.h" |
17 #include "net/proxy/proxy_server.h" | 18 #include "net/proxy/proxy_server.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 Error error_ = ERR_FAILED; | 54 Error error_ = ERR_FAILED; |
54 mojo::Array<interfaces::ProxyServerPtr> results_; | 55 mojo::Array<interfaces::ProxyServerPtr> results_; |
55 | 56 |
56 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 57 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
57 | 58 |
58 EventWaiter<Event> event_waiter_; | 59 EventWaiter<Event> event_waiter_; |
59 }; | 60 }; |
60 | 61 |
61 TestRequestClient::TestRequestClient( | 62 TestRequestClient::TestRequestClient( |
62 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) | 63 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) |
63 : binding_(this, request.Pass()) { | 64 : binding_(this, std::move(request)) { |
64 binding_.set_connection_error_handler(base::Bind( | 65 binding_.set_connection_error_handler(base::Bind( |
65 &TestRequestClient::OnConnectionError, base::Unretained(this))); | 66 &TestRequestClient::OnConnectionError, base::Unretained(this))); |
66 } | 67 } |
67 | 68 |
68 void TestRequestClient::WaitForResult() { | 69 void TestRequestClient::WaitForResult() { |
69 if (done_) | 70 if (done_) |
70 return; | 71 return; |
71 | 72 |
72 event_waiter_.WaitForEvent(RESULT_RECEIVED); | 73 event_waiter_.WaitForEvent(RESULT_RECEIVED); |
73 ASSERT_TRUE(done_); | 74 ASSERT_TRUE(done_); |
74 } | 75 } |
75 | 76 |
76 void TestRequestClient::ReportResult( | 77 void TestRequestClient::ReportResult( |
77 int32_t error, | 78 int32_t error, |
78 mojo::Array<interfaces::ProxyServerPtr> results) { | 79 mojo::Array<interfaces::ProxyServerPtr> results) { |
79 event_waiter_.NotifyEvent(RESULT_RECEIVED); | 80 event_waiter_.NotifyEvent(RESULT_RECEIVED); |
80 ASSERT_FALSE(done_); | 81 ASSERT_FALSE(done_); |
81 error_ = static_cast<Error>(error); | 82 error_ = static_cast<Error>(error); |
82 results_ = results.Pass(); | 83 results_ = std::move(results); |
83 done_ = true; | 84 done_ = true; |
84 } | 85 } |
85 | 86 |
86 void TestRequestClient::Alert(const mojo::String& message) { | 87 void TestRequestClient::Alert(const mojo::String& message) { |
87 } | 88 } |
88 | 89 |
89 void TestRequestClient::OnError(int32_t line_number, | 90 void TestRequestClient::OnError(int32_t line_number, |
90 const mojo::String& message) { | 91 const mojo::String& message) { |
91 } | 92 } |
92 | 93 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 171 } |
171 | 172 |
172 } // namespace | 173 } // namespace |
173 | 174 |
174 class MojoProxyResolverImplTest : public testing::Test { | 175 class MojoProxyResolverImplTest : public testing::Test { |
175 protected: | 176 protected: |
176 void SetUp() override { | 177 void SetUp() override { |
177 scoped_ptr<MockProxyResolverV8Tracing> mock_resolver( | 178 scoped_ptr<MockProxyResolverV8Tracing> mock_resolver( |
178 new MockProxyResolverV8Tracing); | 179 new MockProxyResolverV8Tracing); |
179 mock_proxy_resolver_ = mock_resolver.get(); | 180 mock_proxy_resolver_ = mock_resolver.get(); |
180 resolver_impl_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); | 181 resolver_impl_.reset(new MojoProxyResolverImpl(std::move(mock_resolver))); |
181 resolver_ = resolver_impl_.get(); | 182 resolver_ = resolver_impl_.get(); |
182 } | 183 } |
183 | 184 |
184 MockProxyResolverV8Tracing* mock_proxy_resolver_; | 185 MockProxyResolverV8Tracing* mock_proxy_resolver_; |
185 | 186 |
186 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; | 187 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; |
187 interfaces::ProxyResolver* resolver_; | 188 interfaces::ProxyResolver* resolver_; |
188 }; | 189 }; |
189 | 190 |
190 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 191 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
191 interfaces::ProxyResolverRequestClientPtr client_ptr; | 192 interfaces::ProxyResolverRequestClientPtr client_ptr; |
192 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 193 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
193 | 194 |
194 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 195 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
195 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 196 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
196 const MockProxyResolverV8Tracing::Request& request = | 197 const MockProxyResolverV8Tracing::Request& request = |
197 mock_proxy_resolver_->pending_requests()[0]; | 198 mock_proxy_resolver_->pending_requests()[0]; |
198 EXPECT_EQ(GURL("http://example.com"), request.url); | 199 EXPECT_EQ(GURL("http://example.com"), request.url); |
199 | 200 |
200 request.results->UsePacString( | 201 request.results->UsePacString( |
201 "PROXY proxy.example.com:1; " | 202 "PROXY proxy.example.com:1; " |
202 "SOCKS4 socks4.example.com:2; " | 203 "SOCKS4 socks4.example.com:2; " |
203 "SOCKS5 socks5.example.com:3; " | 204 "SOCKS5 socks5.example.com:3; " |
204 "HTTPS https.example.com:4; " | 205 "HTTPS https.example.com:4; " |
(...skipping 26 matching lines...) Expand all Loading... |
231 EXPECT_EQ("quic.example.com", servers[4].host_port_pair().host()); | 232 EXPECT_EQ("quic.example.com", servers[4].host_port_pair().host()); |
232 EXPECT_EQ(65000, servers[4].host_port_pair().port()); | 233 EXPECT_EQ(65000, servers[4].host_port_pair().port()); |
233 | 234 |
234 EXPECT_EQ(ProxyServer::SCHEME_DIRECT, servers[5].scheme()); | 235 EXPECT_EQ(ProxyServer::SCHEME_DIRECT, servers[5].scheme()); |
235 } | 236 } |
236 | 237 |
237 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { | 238 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { |
238 interfaces::ProxyResolverRequestClientPtr client_ptr; | 239 interfaces::ProxyResolverRequestClientPtr client_ptr; |
239 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 240 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
240 | 241 |
241 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 242 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
242 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 243 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
243 const MockProxyResolverV8Tracing::Request& request = | 244 const MockProxyResolverV8Tracing::Request& request = |
244 mock_proxy_resolver_->pending_requests()[0]; | 245 mock_proxy_resolver_->pending_requests()[0]; |
245 EXPECT_EQ(GURL("http://example.com"), request.url); | 246 EXPECT_EQ(GURL("http://example.com"), request.url); |
246 request.callback.Run(ERR_FAILED); | 247 request.callback.Run(ERR_FAILED); |
247 client.WaitForResult(); | 248 client.WaitForResult(); |
248 | 249 |
249 EXPECT_EQ(ERR_FAILED, client.error()); | 250 EXPECT_EQ(ERR_FAILED, client.error()); |
250 std::vector<ProxyServer> proxy_servers = | 251 std::vector<ProxyServer> proxy_servers = |
251 client.results().To<std::vector<ProxyServer>>(); | 252 client.results().To<std::vector<ProxyServer>>(); |
252 EXPECT_TRUE(proxy_servers.empty()); | 253 EXPECT_TRUE(proxy_servers.empty()); |
253 } | 254 } |
254 | 255 |
255 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { | 256 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { |
256 interfaces::ProxyResolverRequestClientPtr client_ptr1; | 257 interfaces::ProxyResolverRequestClientPtr client_ptr1; |
257 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); | 258 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); |
258 interfaces::ProxyResolverRequestClientPtr client_ptr2; | 259 interfaces::ProxyResolverRequestClientPtr client_ptr2; |
259 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); | 260 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); |
260 | 261 |
261 resolver_->GetProxyForUrl("http://example.com", client_ptr1.Pass()); | 262 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr1)); |
262 resolver_->GetProxyForUrl("https://example.com", client_ptr2.Pass()); | 263 resolver_->GetProxyForUrl("https://example.com", std::move(client_ptr2)); |
263 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); | 264 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); |
264 const MockProxyResolverV8Tracing::Request& request1 = | 265 const MockProxyResolverV8Tracing::Request& request1 = |
265 mock_proxy_resolver_->pending_requests()[0]; | 266 mock_proxy_resolver_->pending_requests()[0]; |
266 EXPECT_EQ(GURL("http://example.com"), request1.url); | 267 EXPECT_EQ(GURL("http://example.com"), request1.url); |
267 const MockProxyResolverV8Tracing::Request& request2 = | 268 const MockProxyResolverV8Tracing::Request& request2 = |
268 mock_proxy_resolver_->pending_requests()[1]; | 269 mock_proxy_resolver_->pending_requests()[1]; |
269 EXPECT_EQ(GURL("https://example.com"), request2.url); | 270 EXPECT_EQ(GURL("https://example.com"), request2.url); |
270 request1.results->UsePacString("HTTPS proxy.example.com:12345"); | 271 request1.results->UsePacString("HTTPS proxy.example.com:12345"); |
271 request1.callback.Run(OK); | 272 request1.callback.Run(OK); |
272 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); | 273 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); |
(...skipping 18 matching lines...) Expand all Loading... |
291 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); | 292 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); |
292 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); | 293 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); |
293 EXPECT_EQ(6789, server2.host_port_pair().port()); | 294 EXPECT_EQ(6789, server2.host_port_pair().port()); |
294 } | 295 } |
295 | 296 |
296 TEST_F(MojoProxyResolverImplTest, DestroyClient) { | 297 TEST_F(MojoProxyResolverImplTest, DestroyClient) { |
297 interfaces::ProxyResolverRequestClientPtr client_ptr; | 298 interfaces::ProxyResolverRequestClientPtr client_ptr; |
298 scoped_ptr<TestRequestClient> client( | 299 scoped_ptr<TestRequestClient> client( |
299 new TestRequestClient(mojo::GetProxy(&client_ptr))); | 300 new TestRequestClient(mojo::GetProxy(&client_ptr))); |
300 | 301 |
301 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 302 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
302 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 303 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
303 const MockProxyResolverV8Tracing::Request& request = | 304 const MockProxyResolverV8Tracing::Request& request = |
304 mock_proxy_resolver_->pending_requests()[0]; | 305 mock_proxy_resolver_->pending_requests()[0]; |
305 EXPECT_EQ(GURL("http://example.com"), request.url); | 306 EXPECT_EQ(GURL("http://example.com"), request.url); |
306 request.results->UsePacString("PROXY proxy.example.com:8080"); | 307 request.results->UsePacString("PROXY proxy.example.com:8080"); |
307 client.reset(); | 308 client.reset(); |
308 mock_proxy_resolver_->WaitForCancel(); | 309 mock_proxy_resolver_->WaitForCancel(); |
309 } | 310 } |
310 | 311 |
311 TEST_F(MojoProxyResolverImplTest, DestroyService) { | 312 TEST_F(MojoProxyResolverImplTest, DestroyService) { |
312 interfaces::ProxyResolverRequestClientPtr client_ptr; | 313 interfaces::ProxyResolverRequestClientPtr client_ptr; |
313 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 314 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
314 | 315 |
315 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 316 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
316 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 317 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
317 resolver_impl_.reset(); | 318 resolver_impl_.reset(); |
318 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 319 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
319 } | 320 } |
320 | 321 |
321 } // namespace net | 322 } // namespace net |
OLD | NEW |