| 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/proxy_resolver_mojo.h" | 5 #include "net/proxy/proxy_resolver_mojo.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
| 16 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 17 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 18 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 19 #include "net/proxy/mojo_proxy_resolver_factory.h" | 20 #include "net/proxy/mojo_proxy_resolver_factory.h" |
| 20 #include "net/proxy/mojo_type_converters.h" | 21 #include "net/proxy/mojo_type_converters.h" |
| 21 #include "net/proxy/proxy_info.h" | 22 #include "net/proxy/proxy_info.h" |
| 22 #include "net/proxy/proxy_resolver_script_data.h" | 23 #include "net/proxy/proxy_resolver_script_data.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 67 |
| 67 void AddPacScriptAction(SetPacScriptAction action); | 68 void AddPacScriptAction(SetPacScriptAction action); |
| 68 // Returned script data is UTF8. | 69 // Returned script data is UTF8. |
| 69 std::string GetPacScriptData() { return pac_script_data_; } | 70 std::string GetPacScriptData() { return pac_script_data_; } |
| 70 int GetPacScriptCalls() { return pac_script_calls_; } | 71 int GetPacScriptCalls() { return pac_script_calls_; } |
| 71 | 72 |
| 72 void AddProxyResult(const GURL& url, | 73 void AddProxyResult(const GURL& url, |
| 73 int error, | 74 int error, |
| 74 mojo::Array<interfaces::ProxyServerPtr> proxy_servers); | 75 mojo::Array<interfaces::ProxyServerPtr> proxy_servers); |
| 75 void CloseRequestClient(const GURL& url); | 76 void CloseRequestClient(const GURL& url); |
| 77 void BlockProxyRequest(const GURL& url); |
| 76 | 78 |
| 77 void Disconnect() { binding_.Close(); } | 79 void Disconnect() { binding_.Close(); } |
| 78 | 80 |
| 79 void WaitForSetPacScript(); | 81 void WaitForSetPacScript(); |
| 80 | 82 |
| 83 void ClearBlockedClients(); |
| 84 |
| 81 private: | 85 private: |
| 82 struct ProxyResults { | 86 struct ProxyResults { |
| 83 explicit ProxyResults(bool close) | 87 ProxyResults(bool close, bool block) |
| 84 : close(close), error(OK), proxy_servers() {} | 88 : close(close), block(block), error(OK), proxy_servers() {} |
| 85 ProxyResults(int err, mojo::Array<interfaces::ProxyServerPtr>& s) | 89 ProxyResults(int err, mojo::Array<interfaces::ProxyServerPtr>& s) |
| 86 : close(false), error(err), proxy_servers(s.Pass()) {} | 90 : close(false), block(false), error(err), proxy_servers(s.Pass()) {} |
| 87 | 91 |
| 88 bool close; | 92 bool close; |
| 93 bool block; |
| 89 int error; | 94 int error; |
| 90 mojo::Array<interfaces::ProxyServerPtr> proxy_servers; | 95 mojo::Array<interfaces::ProxyServerPtr> proxy_servers; |
| 91 }; | 96 }; |
| 92 | 97 |
| 93 // Overridden from interfaces::ProxyResolver: | 98 // Overridden from interfaces::ProxyResolver: |
| 94 void SetPacScript(const mojo::String& data, | 99 void SetPacScript(const mojo::String& data, |
| 95 const mojo::Callback<void(int32_t)>& callback) override; | 100 const mojo::Callback<void(int32_t)>& callback) override; |
| 96 void GetProxyForUrl( | 101 void GetProxyForUrl( |
| 97 const mojo::String& url, | 102 const mojo::String& url, |
| 98 interfaces::ProxyResolverRequestClientPtr client) override; | 103 interfaces::ProxyResolverRequestClientPtr client) override; |
| 99 | 104 |
| 100 std::string pac_script_data_; | 105 std::string pac_script_data_; |
| 101 std::list<SetPacScriptAction> pac_script_actions_; | 106 std::list<SetPacScriptAction> pac_script_actions_; |
| 102 int pac_script_calls_; | 107 int pac_script_calls_; |
| 103 base::Closure pac_script_quit_closure_; | 108 base::Closure pac_script_quit_closure_; |
| 104 | 109 |
| 105 std::map<GURL, ProxyResults*> proxy_results_; | 110 std::map<GURL, ProxyResults*> proxy_results_; |
| 106 | 111 |
| 112 ScopedVector<interfaces::ProxyResolverRequestClientPtr> blocked_clients_; |
| 107 mojo::Binding<interfaces::ProxyResolver> binding_; | 113 mojo::Binding<interfaces::ProxyResolver> binding_; |
| 108 }; | 114 }; |
| 109 | 115 |
| 110 void MockMojoProxyResolver::AddProxyResult( | 116 void MockMojoProxyResolver::AddProxyResult( |
| 111 const GURL& url, | 117 const GURL& url, |
| 112 int error, | 118 int error, |
| 113 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { | 119 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { |
| 114 proxy_results_[url] = new ProxyResults(error, proxy_servers); | 120 proxy_results_[url] = new ProxyResults(error, proxy_servers); |
| 115 } | 121 } |
| 116 | 122 |
| 117 void MockMojoProxyResolver::CloseRequestClient(const GURL& url) { | 123 void MockMojoProxyResolver::CloseRequestClient(const GURL& url) { |
| 118 proxy_results_[url] = new ProxyResults(true); | 124 proxy_results_[url] = new ProxyResults(true, false); |
| 125 } |
| 126 |
| 127 void MockMojoProxyResolver::BlockProxyRequest(const GURL& url) { |
| 128 proxy_results_[url] = new ProxyResults(false, true); |
| 119 } | 129 } |
| 120 | 130 |
| 121 void MockMojoProxyResolver::AddPacScriptAction(SetPacScriptAction action) { | 131 void MockMojoProxyResolver::AddPacScriptAction(SetPacScriptAction action) { |
| 122 pac_script_actions_.push_back(action); | 132 pac_script_actions_.push_back(action); |
| 123 } | 133 } |
| 124 | 134 |
| 125 void MockMojoProxyResolver::SetPacScript( | 135 void MockMojoProxyResolver::SetPacScript( |
| 126 const mojo::String& data, | 136 const mojo::String& data, |
| 127 const mojo::Callback<void(int32_t)>& callback) { | 137 const mojo::Callback<void(int32_t)>& callback) { |
| 128 pac_script_data_ = data.To<std::string>(); | 138 pac_script_data_ = data.To<std::string>(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 148 | 158 |
| 149 void MockMojoProxyResolver::WaitForSetPacScript() { | 159 void MockMojoProxyResolver::WaitForSetPacScript() { |
| 150 if (pac_script_calls_) | 160 if (pac_script_calls_) |
| 151 return; | 161 return; |
| 152 | 162 |
| 153 base::RunLoop run_loop; | 163 base::RunLoop run_loop; |
| 154 pac_script_quit_closure_ = run_loop.QuitClosure(); | 164 pac_script_quit_closure_ = run_loop.QuitClosure(); |
| 155 run_loop.Run(); | 165 run_loop.Run(); |
| 156 } | 166 } |
| 157 | 167 |
| 168 void MockMojoProxyResolver::ClearBlockedClients() { |
| 169 blocked_clients_.clear(); |
| 170 } |
| 171 |
| 158 void MockMojoProxyResolver::GetProxyForUrl( | 172 void MockMojoProxyResolver::GetProxyForUrl( |
| 159 const mojo::String& url, | 173 const mojo::String& url, |
| 160 interfaces::ProxyResolverRequestClientPtr client) { | 174 interfaces::ProxyResolverRequestClientPtr client) { |
| 161 ProxyResults* result = proxy_results_[url.To<GURL>()]; | 175 ProxyResults* result = proxy_results_[url.To<GURL>()]; |
| 162 ASSERT_NE(nullptr, result); | 176 ASSERT_NE(nullptr, result); |
| 163 if (result->close) { | 177 if (result->close) { |
| 164 client.reset(); | 178 client.reset(); |
| 179 } else if (result->block) { |
| 180 client->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); |
| 181 blocked_clients_.push_back( |
| 182 new interfaces::ProxyResolverRequestClientPtr(client.Pass())); |
| 165 } else { | 183 } else { |
| 166 client->ReportResult(result->error, result->proxy_servers.Clone()); | 184 client->ReportResult(result->error, result->proxy_servers.Clone()); |
| 167 } | 185 } |
| 168 } | 186 } |
| 169 | 187 |
| 170 class TestMojoProxyResolverFactory : public MojoProxyResolverFactory { | 188 class TestMojoProxyResolverFactory : public MojoProxyResolverFactory { |
| 171 public: | 189 public: |
| 172 TestMojoProxyResolverFactory(); | 190 TestMojoProxyResolverFactory(); |
| 173 ~TestMojoProxyResolverFactory(); | 191 ~TestMojoProxyResolverFactory(); |
| 174 | 192 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 class Request { | 249 class Request { |
| 232 public: | 250 public: |
| 233 Request(ProxyResolverMojo* resolver, const GURL& url); | 251 Request(ProxyResolverMojo* resolver, const GURL& url); |
| 234 | 252 |
| 235 int Resolve(); | 253 int Resolve(); |
| 236 void Cancel(); | 254 void Cancel(); |
| 237 int WaitForResult(); | 255 int WaitForResult(); |
| 238 | 256 |
| 239 int error() const { return error_; } | 257 int error() const { return error_; } |
| 240 const ProxyInfo& results() const { return results_; } | 258 const ProxyInfo& results() const { return results_; } |
| 259 LoadState load_state() { return resolver_->GetLoadState(handle_); } |
| 241 | 260 |
| 242 private: | 261 private: |
| 243 // Completion callback for ProxyResolverMojo::Resolve. | 262 // Completion callback for ProxyResolverMojo::Resolve. |
| 244 void OnResolveDone(int error); | 263 void OnResolveDone(int error); |
| 245 | 264 |
| 246 ProxyResolverMojo* resolver_; | 265 ProxyResolverMojo* resolver_; |
| 247 const GURL url_; | 266 const GURL url_; |
| 248 ProxyInfo results_; | 267 ProxyInfo results_; |
| 249 ProxyResolver::RequestHandle handle_; | 268 ProxyResolver::RequestHandle handle_; |
| 250 int error_; | 269 int error_; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 TEST_F(ProxyResolverMojoTest, GetProxyForURL) { | 451 TEST_F(ProxyResolverMojoTest, GetProxyForURL) { |
| 433 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); | 452 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); |
| 434 mojo_proxy_resolver_factory_.GetMockResolver()->AddProxyResult( | 453 mojo_proxy_resolver_factory_.GetMockResolver()->AddProxyResult( |
| 435 GURL("http://www.example.com"), OK, ProxyServersFromPacString("DIRECT")); | 454 GURL("http://www.example.com"), OK, ProxyServersFromPacString("DIRECT")); |
| 436 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 455 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); |
| 437 EXPECT_EQ(OK, request->WaitForResult()); | 456 EXPECT_EQ(OK, request->WaitForResult()); |
| 438 | 457 |
| 439 EXPECT_EQ("DIRECT", request->results().ToPacString()); | 458 EXPECT_EQ("DIRECT", request->results().ToPacString()); |
| 440 } | 459 } |
| 441 | 460 |
| 461 TEST_F(ProxyResolverMojoTest, GetProxyForURL_LoadState) { |
| 462 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); |
| 463 mojo_proxy_resolver_factory_.GetMockResolver()->BlockProxyRequest( |
| 464 GURL("http://www.example.com")); |
| 465 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); |
| 466 EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL, request->load_state()); |
| 467 while (request->load_state() == LOAD_STATE_RESOLVING_PROXY_FOR_URL) |
| 468 base::RunLoop().RunUntilIdle(); |
| 469 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, request->load_state()); |
| 470 mojo_proxy_resolver_factory_.GetMockResolver()->ClearBlockedClients(); |
| 471 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, request->WaitForResult()); |
| 472 } |
| 473 |
| 442 TEST_F(ProxyResolverMojoTest, GetProxyForURL_MultipleResults) { | 474 TEST_F(ProxyResolverMojoTest, GetProxyForURL_MultipleResults) { |
| 443 static const char kPacString[] = | 475 static const char kPacString[] = |
| 444 "PROXY foo1:80;DIRECT;SOCKS foo2:1234;" | 476 "PROXY foo1:80;DIRECT;SOCKS foo2:1234;" |
| 445 "SOCKS5 foo3:1080;HTTPS foo4:443;QUIC foo6:8888"; | 477 "SOCKS5 foo3:1080;HTTPS foo4:443;QUIC foo6:8888"; |
| 446 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); | 478 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); |
| 447 mojo_proxy_resolver_factory_.GetMockResolver()->AddProxyResult( | 479 mojo_proxy_resolver_factory_.GetMockResolver()->AddProxyResult( |
| 448 GURL("http://www.example.com"), OK, | 480 GURL("http://www.example.com"), OK, |
| 449 ProxyServersFromPacString(kPacString)); | 481 ProxyServersFromPacString(kPacString)); |
| 450 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 482 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); |
| 451 EXPECT_EQ(OK, request->WaitForResult()); | 483 EXPECT_EQ(OK, request->WaitForResult()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); | 553 scoped_ptr<Request> request(MakeRequest(GURL("http://www.example.com"))); |
| 522 mojo_proxy_resolver_factory_.GetMockResolver()->CloseRequestClient( | 554 mojo_proxy_resolver_factory_.GetMockResolver()->CloseRequestClient( |
| 523 GURL("http://www.example.com")); | 555 GURL("http://www.example.com")); |
| 524 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 556 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); |
| 525 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, request->WaitForResult()); | 557 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, request->WaitForResult()); |
| 526 | 558 |
| 527 EXPECT_TRUE(request->results().is_empty()); | 559 EXPECT_TRUE(request->results().is_empty()); |
| 528 } | 560 } |
| 529 | 561 |
| 530 } // namespace net | 562 } // namespace net |
| OLD | NEW |