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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/proxy/mock_proxy_resolver.h" | 13 #include "net/proxy/mock_proxy_resolver.h" |
14 #include "net/proxy/mojo_proxy_type_converters.h" | 14 #include "net/proxy/mojo_proxy_type_converters.h" |
15 #include "net/proxy/proxy_info.h" | 15 #include "net/proxy/proxy_info.h" |
16 #include "net/proxy/proxy_server.h" | 16 #include "net/proxy/proxy_server.h" |
| 17 #include "net/test/event_waiter.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | 19 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 namespace { | 23 namespace { |
23 | 24 |
24 class TestRequestClient : public interfaces::ProxyResolverRequestClient, | 25 class TestRequestClient : public interfaces::ProxyResolverRequestClient, |
25 public mojo::ErrorHandler { | 26 public mojo::ErrorHandler { |
26 public: | 27 public: |
| 28 enum Event { |
| 29 RESULT_RECEIVED, |
| 30 LOAD_STATE_CHANGED, |
| 31 CONNECTION_ERROR, |
| 32 }; |
| 33 |
27 explicit TestRequestClient( | 34 explicit TestRequestClient( |
28 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); | 35 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); |
29 | 36 |
30 void WaitForResult(); | 37 void WaitForResult(); |
31 void WaitForConnectionError(); | |
32 | 38 |
33 Error error() { return error_; } | 39 Error error() { return error_; } |
34 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } | 40 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } |
| 41 LoadState load_state() { return load_state_; } |
| 42 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
35 | 43 |
36 private: | 44 private: |
37 // interfaces::ProxyResolverRequestClient override. | 45 // interfaces::ProxyResolverRequestClient override. |
38 void ReportResult(int32_t error, | 46 void ReportResult(int32_t error, |
39 mojo::Array<interfaces::ProxyServerPtr> results) override; | 47 mojo::Array<interfaces::ProxyServerPtr> results) override; |
| 48 void LoadStateChanged(int32_t load_state) override; |
40 | 49 |
41 // mojo::ErrorHandler override. | 50 // mojo::ErrorHandler override. |
42 void OnConnectionError() override; | 51 void OnConnectionError() override; |
43 | 52 |
44 bool done_ = false; | 53 bool done_ = false; |
45 bool encountered_connection_error_ = false; | |
46 Error error_ = ERR_FAILED; | 54 Error error_ = ERR_FAILED; |
| 55 LoadState load_state_ = LOAD_STATE_IDLE; |
47 mojo::Array<interfaces::ProxyServerPtr> results_; | 56 mojo::Array<interfaces::ProxyServerPtr> results_; |
48 base::Closure run_loop_quit_closure_; | |
49 base::Closure connection_error_callback_; | |
50 | 57 |
51 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 58 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
| 59 |
| 60 EventWaiter<Event> event_waiter_; |
52 }; | 61 }; |
53 | 62 |
54 TestRequestClient::TestRequestClient( | 63 TestRequestClient::TestRequestClient( |
55 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) | 64 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) |
56 : binding_(this, request.Pass()) { | 65 : binding_(this, request.Pass()) { |
57 binding_.set_error_handler(this); | 66 binding_.set_error_handler(this); |
58 } | 67 } |
59 | 68 |
60 void TestRequestClient::WaitForResult() { | 69 void TestRequestClient::WaitForResult() { |
61 if (done_) | 70 if (done_) |
62 return; | 71 return; |
63 | 72 |
64 base::RunLoop run_loop; | 73 event_waiter_.WaitForEvent(RESULT_RECEIVED); |
65 run_loop_quit_closure_ = run_loop.QuitClosure(); | |
66 run_loop.Run(); | |
67 ASSERT_TRUE(done_); | 74 ASSERT_TRUE(done_); |
68 } | 75 } |
69 | 76 |
70 void TestRequestClient::WaitForConnectionError() { | |
71 if (encountered_connection_error_) | |
72 return; | |
73 | |
74 base::RunLoop run_loop; | |
75 connection_error_callback_ = run_loop.QuitClosure(); | |
76 run_loop.Run(); | |
77 ASSERT_TRUE(encountered_connection_error_); | |
78 } | |
79 | |
80 void TestRequestClient::ReportResult( | 77 void TestRequestClient::ReportResult( |
81 int32_t error, | 78 int32_t error, |
82 mojo::Array<interfaces::ProxyServerPtr> results) { | 79 mojo::Array<interfaces::ProxyServerPtr> results) { |
83 if (!run_loop_quit_closure_.is_null()) { | 80 event_waiter_.NotifyEvent(RESULT_RECEIVED); |
84 run_loop_quit_closure_.Run(); | |
85 } | |
86 ASSERT_FALSE(done_); | 81 ASSERT_FALSE(done_); |
87 error_ = static_cast<Error>(error); | 82 error_ = static_cast<Error>(error); |
88 results_ = results.Pass(); | 83 results_ = results.Pass(); |
89 done_ = true; | 84 done_ = true; |
90 } | 85 } |
91 | 86 |
| 87 void TestRequestClient::LoadStateChanged(int32_t load_state) { |
| 88 event_waiter_.NotifyEvent(LOAD_STATE_CHANGED); |
| 89 load_state_ = static_cast<LoadState>(load_state); |
| 90 } |
| 91 |
92 void TestRequestClient::OnConnectionError() { | 92 void TestRequestClient::OnConnectionError() { |
93 if (!connection_error_callback_.is_null()) | 93 event_waiter_.NotifyEvent(CONNECTION_ERROR); |
94 connection_error_callback_.Run(); | |
95 encountered_connection_error_ = true; | |
96 } | 94 } |
97 | 95 |
98 class SetPacScriptClient { | 96 class SetPacScriptClient { |
99 public: | 97 public: |
100 base::Callback<void(int32_t)> CreateCallback(); | 98 base::Callback<void(int32_t)> CreateCallback(); |
101 Error error() { return error_; } | 99 Error error() { return error_; } |
102 | 100 |
103 private: | 101 private: |
104 void ReportResult(int32_t error); | 102 void ReportResult(int32_t error); |
105 | 103 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 202 } |
205 | 203 |
206 } // namespace | 204 } // namespace |
207 | 205 |
208 class MojoProxyResolverImplTest : public testing::Test { | 206 class MojoProxyResolverImplTest : public testing::Test { |
209 protected: | 207 protected: |
210 void SetUp() override { | 208 void SetUp() override { |
211 scoped_ptr<CallbackMockProxyResolver> mock_resolver( | 209 scoped_ptr<CallbackMockProxyResolver> mock_resolver( |
212 new CallbackMockProxyResolver); | 210 new CallbackMockProxyResolver); |
213 mock_proxy_resolver_ = mock_resolver.get(); | 211 mock_proxy_resolver_ = mock_resolver.get(); |
214 resolver_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); | 212 resolver_impl_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); |
| 213 resolver_ = resolver_impl_.get(); |
215 } | 214 } |
216 | 215 |
217 CallbackMockProxyResolver* mock_proxy_resolver_; | 216 CallbackMockProxyResolver* mock_proxy_resolver_; |
218 | 217 |
219 scoped_ptr<interfaces::ProxyResolver> resolver_; | 218 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; |
| 219 interfaces::ProxyResolver* resolver_; |
220 }; | 220 }; |
221 | 221 |
222 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 222 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
223 interfaces::ProxyResolverRequestClientPtr client_ptr; | 223 interfaces::ProxyResolverRequestClientPtr client_ptr; |
224 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 224 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
225 | 225 |
226 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 226 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
227 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 227 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
228 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 228 scoped_refptr<MockAsyncProxyResolverBase::Request> request = |
229 mock_proxy_resolver_->pending_requests()[0]; | 229 mock_proxy_resolver_->pending_requests()[0]; |
230 EXPECT_EQ(GURL("http://example.com"), request->url()); | 230 EXPECT_EQ(GURL("http://example.com"), request->url()); |
| 231 |
| 232 resolver_impl_->LoadStateChanged(request.get(), |
| 233 LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); |
| 234 client.event_waiter().WaitForEvent(TestRequestClient::LOAD_STATE_CHANGED); |
| 235 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, client.load_state()); |
| 236 |
231 request->results()->UsePacString( | 237 request->results()->UsePacString( |
232 "PROXY proxy.example.com:1; " | 238 "PROXY proxy.example.com:1; " |
233 "SOCKS4 socks4.example.com:2; " | 239 "SOCKS4 socks4.example.com:2; " |
234 "SOCKS5 socks5.example.com:3; " | 240 "SOCKS5 socks5.example.com:3; " |
235 "HTTPS https.example.com:4; " | 241 "HTTPS https.example.com:4; " |
236 "QUIC quic.example.com:65000; " | 242 "QUIC quic.example.com:65000; " |
237 "DIRECT"); | 243 "DIRECT"); |
238 request->CompleteNow(OK); | 244 request->CompleteNow(OK); |
239 client.WaitForResult(); | 245 client.WaitForResult(); |
240 | 246 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 408 |
403 TEST_F(MojoProxyResolverImplTest, DestroyService) { | 409 TEST_F(MojoProxyResolverImplTest, DestroyService) { |
404 interfaces::ProxyResolverRequestClientPtr client_ptr; | 410 interfaces::ProxyResolverRequestClientPtr client_ptr; |
405 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 411 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
406 | 412 |
407 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 413 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
408 resolver_->SetPacScript("pac script", base::Bind(&Fail)); | 414 resolver_->SetPacScript("pac script", base::Bind(&Fail)); |
409 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 415 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
410 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 416 scoped_refptr<MockAsyncProxyResolverBase::Request> request = |
411 mock_proxy_resolver_->pending_requests()[0]; | 417 mock_proxy_resolver_->pending_requests()[0]; |
412 resolver_.reset(); | 418 resolver_impl_.reset(); |
413 client.WaitForConnectionError(); | 419 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
414 } | 420 } |
415 | 421 |
416 } // namespace net | 422 } // namespace net |
OLD | NEW |