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 "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/proxy/mock_proxy_resolver.h" | 12 #include "net/proxy/mock_proxy_resolver.h" |
13 #include "net/proxy/mojo_proxy_type_converters.h" | 13 #include "net/proxy/mojo_proxy_type_converters.h" |
14 #include "net/proxy/proxy_info.h" | 14 #include "net/proxy/proxy_info.h" |
15 #include "net/proxy/proxy_server.h" | 15 #include "net/proxy/proxy_server.h" |
16 #include "net/test/event_waiter.h" | 16 #include "net/test/event_waiter.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | 18 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | 19 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 namespace { | 22 namespace { |
23 | 23 |
24 class TestRequestClient : public interfaces::ProxyResolverRequestClient, | 24 class TestRequestClient : public interfaces::ProxyResolverRequestClient, |
25 public mojo::ErrorHandler { | 25 public mojo::ErrorHandler { |
26 public: | 26 public: |
27 enum Event { | 27 enum Event { |
28 RESULT_RECEIVED, | 28 RESULT_RECEIVED, |
29 LOAD_STATE_CHANGED, | |
30 CONNECTION_ERROR, | 29 CONNECTION_ERROR, |
31 }; | 30 }; |
32 | 31 |
33 explicit TestRequestClient( | 32 explicit TestRequestClient( |
34 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); | 33 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); |
35 | 34 |
36 void WaitForResult(); | 35 void WaitForResult(); |
37 | 36 |
38 Error error() { return error_; } | 37 Error error() { return error_; } |
39 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } | 38 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } |
40 LoadState load_state() { return load_state_; } | |
41 EventWaiter<Event>& event_waiter() { return event_waiter_; } | 39 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
42 | 40 |
43 private: | 41 private: |
44 // interfaces::ProxyResolverRequestClient override. | 42 // interfaces::ProxyResolverRequestClient override. |
45 void ReportResult(int32_t error, | 43 void ReportResult(int32_t error, |
46 mojo::Array<interfaces::ProxyServerPtr> results) override; | 44 mojo::Array<interfaces::ProxyServerPtr> results) override; |
47 void LoadStateChanged(int32_t load_state) override; | |
48 | 45 |
49 // mojo::ErrorHandler override. | 46 // mojo::ErrorHandler override. |
50 void OnConnectionError() override; | 47 void OnConnectionError() override; |
51 | 48 |
52 bool done_ = false; | 49 bool done_ = false; |
53 Error error_ = ERR_FAILED; | 50 Error error_ = ERR_FAILED; |
54 LoadState load_state_ = LOAD_STATE_IDLE; | |
55 mojo::Array<interfaces::ProxyServerPtr> results_; | 51 mojo::Array<interfaces::ProxyServerPtr> results_; |
56 | 52 |
57 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 53 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
58 | 54 |
59 EventWaiter<Event> event_waiter_; | 55 EventWaiter<Event> event_waiter_; |
60 }; | 56 }; |
61 | 57 |
62 TestRequestClient::TestRequestClient( | 58 TestRequestClient::TestRequestClient( |
63 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) | 59 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) |
64 : binding_(this, request.Pass()) { | 60 : binding_(this, request.Pass()) { |
(...skipping 11 matching lines...) Expand all Loading... |
76 void TestRequestClient::ReportResult( | 72 void TestRequestClient::ReportResult( |
77 int32_t error, | 73 int32_t error, |
78 mojo::Array<interfaces::ProxyServerPtr> results) { | 74 mojo::Array<interfaces::ProxyServerPtr> results) { |
79 event_waiter_.NotifyEvent(RESULT_RECEIVED); | 75 event_waiter_.NotifyEvent(RESULT_RECEIVED); |
80 ASSERT_FALSE(done_); | 76 ASSERT_FALSE(done_); |
81 error_ = static_cast<Error>(error); | 77 error_ = static_cast<Error>(error); |
82 results_ = results.Pass(); | 78 results_ = results.Pass(); |
83 done_ = true; | 79 done_ = true; |
84 } | 80 } |
85 | 81 |
86 void TestRequestClient::LoadStateChanged(int32_t load_state) { | |
87 event_waiter_.NotifyEvent(LOAD_STATE_CHANGED); | |
88 load_state_ = static_cast<LoadState>(load_state); | |
89 } | |
90 | |
91 void TestRequestClient::OnConnectionError() { | 82 void TestRequestClient::OnConnectionError() { |
92 event_waiter_.NotifyEvent(CONNECTION_ERROR); | 83 event_waiter_.NotifyEvent(CONNECTION_ERROR); |
93 } | 84 } |
94 | 85 |
95 class CallbackMockProxyResolver : public MockAsyncProxyResolver { | 86 class CallbackMockProxyResolver : public MockAsyncProxyResolver { |
96 public: | 87 public: |
97 CallbackMockProxyResolver() {} | 88 CallbackMockProxyResolver() {} |
98 ~CallbackMockProxyResolver() override; | 89 ~CallbackMockProxyResolver() override; |
99 | 90 |
100 // MockAsyncProxyResolver overrides. | 91 // MockAsyncProxyResolver overrides. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 } | 148 } |
158 | 149 |
159 } // namespace | 150 } // namespace |
160 | 151 |
161 class MojoProxyResolverImplTest : public testing::Test { | 152 class MojoProxyResolverImplTest : public testing::Test { |
162 protected: | 153 protected: |
163 void SetUp() override { | 154 void SetUp() override { |
164 scoped_ptr<CallbackMockProxyResolver> mock_resolver( | 155 scoped_ptr<CallbackMockProxyResolver> mock_resolver( |
165 new CallbackMockProxyResolver); | 156 new CallbackMockProxyResolver); |
166 mock_proxy_resolver_ = mock_resolver.get(); | 157 mock_proxy_resolver_ = mock_resolver.get(); |
167 resolver_impl_.reset(new MojoProxyResolverImpl( | 158 resolver_impl_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); |
168 mock_resolver.Pass(), | |
169 base::Bind(&MojoProxyResolverImplTest::set_load_state_changed_callback, | |
170 base::Unretained(this)))); | |
171 resolver_ = resolver_impl_.get(); | 159 resolver_ = resolver_impl_.get(); |
172 } | 160 } |
173 | 161 |
174 void set_load_state_changed_callback( | |
175 const ProxyResolver::LoadStateChangedCallback& callback) { | |
176 EXPECT_TRUE(load_state_changed_callback_.is_null()); | |
177 EXPECT_FALSE(callback.is_null()); | |
178 load_state_changed_callback_ = callback; | |
179 } | |
180 | |
181 CallbackMockProxyResolver* mock_proxy_resolver_; | 162 CallbackMockProxyResolver* mock_proxy_resolver_; |
182 | 163 |
183 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; | 164 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; |
184 interfaces::ProxyResolver* resolver_; | 165 interfaces::ProxyResolver* resolver_; |
185 ProxyResolver::LoadStateChangedCallback load_state_changed_callback_; | |
186 }; | 166 }; |
187 | 167 |
188 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 168 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
189 interfaces::ProxyResolverRequestClientPtr client_ptr; | 169 interfaces::ProxyResolverRequestClientPtr client_ptr; |
190 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 170 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
191 | 171 |
192 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 172 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
193 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 173 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
194 scoped_refptr<MockAsyncProxyResolver::Request> request = | 174 scoped_refptr<MockAsyncProxyResolver::Request> request = |
195 mock_proxy_resolver_->pending_requests()[0]; | 175 mock_proxy_resolver_->pending_requests()[0]; |
196 EXPECT_EQ(GURL("http://example.com"), request->url()); | 176 EXPECT_EQ(GURL("http://example.com"), request->url()); |
197 | 177 |
198 ASSERT_FALSE(load_state_changed_callback_.is_null()); | |
199 load_state_changed_callback_.Run(request.get(), | |
200 LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
201 client.event_waiter().WaitForEvent(TestRequestClient::LOAD_STATE_CHANGED); | |
202 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, client.load_state()); | |
203 | |
204 request->results()->UsePacString( | 178 request->results()->UsePacString( |
205 "PROXY proxy.example.com:1; " | 179 "PROXY proxy.example.com:1; " |
206 "SOCKS4 socks4.example.com:2; " | 180 "SOCKS4 socks4.example.com:2; " |
207 "SOCKS5 socks5.example.com:3; " | 181 "SOCKS5 socks5.example.com:3; " |
208 "HTTPS https.example.com:4; " | 182 "HTTPS https.example.com:4; " |
209 "QUIC quic.example.com:65000; " | 183 "QUIC quic.example.com:65000; " |
210 "DIRECT"); | 184 "DIRECT"); |
211 request->CompleteNow(OK); | 185 request->CompleteNow(OK); |
212 client.WaitForResult(); | 186 client.WaitForResult(); |
213 | 187 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 311 |
338 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 312 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
339 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 313 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
340 scoped_refptr<MockAsyncProxyResolver::Request> request = | 314 scoped_refptr<MockAsyncProxyResolver::Request> request = |
341 mock_proxy_resolver_->pending_requests()[0]; | 315 mock_proxy_resolver_->pending_requests()[0]; |
342 resolver_impl_.reset(); | 316 resolver_impl_.reset(); |
343 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 317 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
344 } | 318 } |
345 | 319 |
346 } // namespace net | 320 } // namespace net |
OLD | NEW |