| 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" | 15 #include "net/proxy/mojo_proxy_type_converters.h" |
| 16 #include "net/proxy/proxy_info.h" | 16 #include "net/proxy/proxy_info.h" |
| 17 #include "net/proxy/proxy_resolver_v8_tracing.h" | 17 #include "net/proxy/proxy_resolver_v8_tracing.h" |
| 18 #include "net/proxy/proxy_server.h" | 18 #include "net/proxy/proxy_server.h" |
| 19 #include "net/test/event_waiter.h" | 19 #include "net/test/event_waiter.h" |
| 20 #include "net/test/gtest_util.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 23 |
| 24 using net::test::IsError; |
| 25 using net::test::IsOk; |
| 26 |
| 22 namespace net { | 27 namespace net { |
| 23 namespace { | 28 namespace { |
| 24 | 29 |
| 25 class TestRequestClient : public interfaces::ProxyResolverRequestClient { | 30 class TestRequestClient : public interfaces::ProxyResolverRequestClient { |
| 26 public: | 31 public: |
| 27 enum Event { | 32 enum Event { |
| 28 RESULT_RECEIVED, | 33 RESULT_RECEIVED, |
| 29 CONNECTION_ERROR, | 34 CONNECTION_ERROR, |
| 30 }; | 35 }; |
| 31 | 36 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 request.results->UsePacString( | 206 request.results->UsePacString( |
| 202 "PROXY proxy.example.com:1; " | 207 "PROXY proxy.example.com:1; " |
| 203 "SOCKS4 socks4.example.com:2; " | 208 "SOCKS4 socks4.example.com:2; " |
| 204 "SOCKS5 socks5.example.com:3; " | 209 "SOCKS5 socks5.example.com:3; " |
| 205 "HTTPS https.example.com:4; " | 210 "HTTPS https.example.com:4; " |
| 206 "QUIC quic.example.com:65000; " | 211 "QUIC quic.example.com:65000; " |
| 207 "DIRECT"); | 212 "DIRECT"); |
| 208 request.callback.Run(OK); | 213 request.callback.Run(OK); |
| 209 client.WaitForResult(); | 214 client.WaitForResult(); |
| 210 | 215 |
| 211 EXPECT_EQ(OK, client.error()); | 216 EXPECT_THAT(client.error(), IsOk()); |
| 212 std::vector<ProxyServer> servers = | 217 std::vector<ProxyServer> servers = |
| 213 client.results().To<std::vector<ProxyServer>>(); | 218 client.results().To<std::vector<ProxyServer>>(); |
| 214 ASSERT_EQ(6u, servers.size()); | 219 ASSERT_EQ(6u, servers.size()); |
| 215 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); | 220 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); |
| 216 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); | 221 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); |
| 217 EXPECT_EQ(1, servers[0].host_port_pair().port()); | 222 EXPECT_EQ(1, servers[0].host_port_pair().port()); |
| 218 | 223 |
| 219 EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, servers[1].scheme()); | 224 EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, servers[1].scheme()); |
| 220 EXPECT_EQ("socks4.example.com", servers[1].host_port_pair().host()); | 225 EXPECT_EQ("socks4.example.com", servers[1].host_port_pair().host()); |
| 221 EXPECT_EQ(2, servers[1].host_port_pair().port()); | 226 EXPECT_EQ(2, servers[1].host_port_pair().port()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 240 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 245 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 241 | 246 |
| 242 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); | 247 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); |
| 243 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 248 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 244 const MockProxyResolverV8Tracing::Request& request = | 249 const MockProxyResolverV8Tracing::Request& request = |
| 245 mock_proxy_resolver_->pending_requests()[0]; | 250 mock_proxy_resolver_->pending_requests()[0]; |
| 246 EXPECT_EQ(GURL("http://example.com"), request.url); | 251 EXPECT_EQ(GURL("http://example.com"), request.url); |
| 247 request.callback.Run(ERR_FAILED); | 252 request.callback.Run(ERR_FAILED); |
| 248 client.WaitForResult(); | 253 client.WaitForResult(); |
| 249 | 254 |
| 250 EXPECT_EQ(ERR_FAILED, client.error()); | 255 EXPECT_THAT(client.error(), IsError(ERR_FAILED)); |
| 251 std::vector<ProxyServer> proxy_servers = | 256 std::vector<ProxyServer> proxy_servers = |
| 252 client.results().To<std::vector<ProxyServer>>(); | 257 client.results().To<std::vector<ProxyServer>>(); |
| 253 EXPECT_TRUE(proxy_servers.empty()); | 258 EXPECT_TRUE(proxy_servers.empty()); |
| 254 } | 259 } |
| 255 | 260 |
| 256 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { | 261 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { |
| 257 interfaces::ProxyResolverRequestClientPtr client_ptr1; | 262 interfaces::ProxyResolverRequestClientPtr client_ptr1; |
| 258 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); | 263 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); |
| 259 interfaces::ProxyResolverRequestClientPtr client_ptr2; | 264 interfaces::ProxyResolverRequestClientPtr client_ptr2; |
| 260 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); | 265 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); |
| 261 | 266 |
| 262 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr1)); | 267 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr1)); |
| 263 resolver_->GetProxyForUrl(GURL("https://example.com"), | 268 resolver_->GetProxyForUrl(GURL("https://example.com"), |
| 264 std::move(client_ptr2)); | 269 std::move(client_ptr2)); |
| 265 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); | 270 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); |
| 266 const MockProxyResolverV8Tracing::Request& request1 = | 271 const MockProxyResolverV8Tracing::Request& request1 = |
| 267 mock_proxy_resolver_->pending_requests()[0]; | 272 mock_proxy_resolver_->pending_requests()[0]; |
| 268 EXPECT_EQ(GURL("http://example.com"), request1.url); | 273 EXPECT_EQ(GURL("http://example.com"), request1.url); |
| 269 const MockProxyResolverV8Tracing::Request& request2 = | 274 const MockProxyResolverV8Tracing::Request& request2 = |
| 270 mock_proxy_resolver_->pending_requests()[1]; | 275 mock_proxy_resolver_->pending_requests()[1]; |
| 271 EXPECT_EQ(GURL("https://example.com"), request2.url); | 276 EXPECT_EQ(GURL("https://example.com"), request2.url); |
| 272 request1.results->UsePacString("HTTPS proxy.example.com:12345"); | 277 request1.results->UsePacString("HTTPS proxy.example.com:12345"); |
| 273 request1.callback.Run(OK); | 278 request1.callback.Run(OK); |
| 274 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); | 279 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); |
| 275 request2.callback.Run(OK); | 280 request2.callback.Run(OK); |
| 276 client1.WaitForResult(); | 281 client1.WaitForResult(); |
| 277 client2.WaitForResult(); | 282 client2.WaitForResult(); |
| 278 | 283 |
| 279 EXPECT_EQ(OK, client1.error()); | 284 EXPECT_THAT(client1.error(), IsOk()); |
| 280 std::vector<ProxyServer> proxy_servers1 = | 285 std::vector<ProxyServer> proxy_servers1 = |
| 281 client1.results().To<std::vector<ProxyServer>>(); | 286 client1.results().To<std::vector<ProxyServer>>(); |
| 282 ASSERT_EQ(1u, proxy_servers1.size()); | 287 ASSERT_EQ(1u, proxy_servers1.size()); |
| 283 ProxyServer& server1 = proxy_servers1[0]; | 288 ProxyServer& server1 = proxy_servers1[0]; |
| 284 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); | 289 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); |
| 285 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); | 290 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); |
| 286 EXPECT_EQ(12345, server1.host_port_pair().port()); | 291 EXPECT_EQ(12345, server1.host_port_pair().port()); |
| 287 | 292 |
| 288 EXPECT_EQ(OK, client2.error()); | 293 EXPECT_THAT(client2.error(), IsOk()); |
| 289 std::vector<ProxyServer> proxy_servers2 = | 294 std::vector<ProxyServer> proxy_servers2 = |
| 290 client2.results().To<std::vector<ProxyServer>>(); | 295 client2.results().To<std::vector<ProxyServer>>(); |
| 291 ASSERT_EQ(1u, proxy_servers1.size()); | 296 ASSERT_EQ(1u, proxy_servers1.size()); |
| 292 ProxyServer& server2 = proxy_servers2[0]; | 297 ProxyServer& server2 = proxy_servers2[0]; |
| 293 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); | 298 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); |
| 294 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); | 299 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); |
| 295 EXPECT_EQ(6789, server2.host_port_pair().port()); | 300 EXPECT_EQ(6789, server2.host_port_pair().port()); |
| 296 } | 301 } |
| 297 | 302 |
| 298 TEST_F(MojoProxyResolverImplTest, DestroyClient) { | 303 TEST_F(MojoProxyResolverImplTest, DestroyClient) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 314 interfaces::ProxyResolverRequestClientPtr client_ptr; | 319 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 315 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 320 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 316 | 321 |
| 317 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); | 322 resolver_->GetProxyForUrl(GURL("http://example.com"), std::move(client_ptr)); |
| 318 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 323 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
| 319 resolver_impl_.reset(); | 324 resolver_impl_.reset(); |
| 320 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 325 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
| 321 } | 326 } |
| 322 | 327 |
| 323 } // namespace net | 328 } // namespace net |
| OLD | NEW |